Skip to content

Commit

Permalink
Initial working version - wrapping OpenFoodFacts API
Browse files Browse the repository at this point in the history
  • Loading branch information
epalmans committed Jan 3, 2020
1 parent b2e3cdc commit 9c71c87
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 97 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ composer require palmans/laravel-openfoodfacts
```

## Usage

Find product details by barcode
``` php
OpenFoodFacts::barcode('20203467');
```
... or just retrieve specific details, e.g.
``` php
// Usage description here
OpenFoodFacts::barcode('20203467')->product_name; // 'Cantuccini with hazelnuts'
OpenFoodFacts::barcode('20203467')->image_url; // 'https://static.openfoodfacts.org/images/products/20203467/front_fr.4.400.jpg'
```

### Testing
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
}
],
"require": {
"php": ">=7",
"illuminate/support": "5.7.x|5.8.x|6.*"
"php": ">=7.1",
"guzzlehttp/guzzle": "^6.3",
"illuminate/support": "5.7.x|5.8.x|6.*",
"openfoodfacts/openfoodfacts-php": "^0.2.0"
},
"require-dev": {
"orchestra/testbench": "3.8.*|4.*",
Expand All @@ -44,10 +46,10 @@
"extra": {
"laravel": {
"providers": [
"Palmans\\LaravelOpenFoodFacts\\LaravelOpenFoodFactsServiceProvider"
"Palmans\\LaravelOpenFoodFacts\\OpenFoodFactsServiceProvider"
],
"aliases": {
"OpenFoodFacts": "Palmans\\LaravelOpenFoodFacts\\OpenFoodFactsFacade"
"OpenFoodFacts": "Palmans\\LaravelOpenFoodFacts\\Facades\\OpenFoodFacts"
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions config/config.php

This file was deleted.

6 changes: 6 additions & 0 deletions config/openfoodfacts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'geography' => 'world',
'max_results' => 1000,
];
13 changes: 13 additions & 0 deletions src/Facades/OpenFoodFacts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Palmans\LaravelOpenFoodFacts\Facades;

use Illuminate\Support\Facades\Facade;

class OpenFoodFacts extends Facade
{
protected static function getFacadeAccessor()
{
return 'openfoodfacts';
}
}
60 changes: 0 additions & 60 deletions src/LaravelOpenFoodFactsServiceProvider.php

This file was deleted.

59 changes: 57 additions & 2 deletions src/OpenFoodFacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,62 @@

namespace Palmans\LaravelOpenFoodFacts;

class OpenFoodFacts
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Collection;
use InvalidArgumentException;

class OpenFoodFacts extends OpenFoodFactsApiWrapper
{
// Build your next great package.
protected $max_results;

public function __construct(Container $app)
{
parent::__construct([
'geography' => $app['config']->get('openfoodfacts.geography'),
'app' => $app['config']->get('app.name'),
], $app['cache.store']);

$this->max_results = $app['config']->get('openfoodfacts.max_results', 1000);
}

public function barcode($value)
{
if (empty($value)) {
throw new InvalidArgumentException("Argument must represent a barcode");
}

return $this->api->getProduct($value);
}

public function find($searchterm)
{
if (empty($searchterm)) {
throw new InvalidArgumentException("Specify a search term to find data for matching products");
}

$products = Collection::make();
$page = 0;

do {
$pageResults = $this->api->search($searchterm, ++$page, 100);
$totalMatches = $pageResults->searchCount();

if ($this->max_results > 0 && $totalMatches > $this->max_results) {
throw new \Exception("ERROR: {$totalMatches} results found, while buffer limited to {$this->max_results}. Please narrow your search.");
}

$pages = (int)ceil($totalMatches / $pageResults->getPageSize());

$products = $products->concat(iterator_to_array($pageResults));

} while ($page < $pages);

return $products;
}

public function __call($method, $parameters)
{
return $this->api->$method(...$parameters);
}
}
41 changes: 41 additions & 0 deletions src/OpenFoodFactsApiWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Palmans\LaravelOpenFoodFacts;

use GuzzleHttp\Client;
use OpenFoodFacts\Api;
use Psr\SimpleCache\CacheInterface;

class OpenFoodFactsApiWrapper
{
protected $parameters;
protected $cache;

public $api;

public function __construct(array $parameters, CacheInterface $cache = null)
{
$this->parameters = $parameters;
$this->cache = $cache;

$this->api = $this->setupApi();
}

protected function setupApi($environment = 'food')
{
return new Api(
$environment,
$this->parameters['geography'] ?? 'world',
null,
$this->httpClient(),
$this->cache
);
}

protected function httpClient()
{
return new Client([
'headers' => ['User-Agent' => $this->parameters['app'] ?? 'Laravel Open Food Facts - https://github.com/palmans/laravel-openfoodfacts'],
]);
}
}
21 changes: 0 additions & 21 deletions src/OpenFoodFactsFacade.php

This file was deleted.

27 changes: 27 additions & 0 deletions src/OpenFoodFactsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Palmans\LaravelOpenFoodFacts;

use Illuminate\Contracts\Container\Container;
use Illuminate\Support\ServiceProvider;

class OpenFoodFactsServiceProvider extends ServiceProvider
{
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/openfoodfacts.php' => config_path('openfoodfacts.php'),
], 'config');
}
}

public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/openfoodfacts.php', 'openfoodfacts');

$this->app->singleton('openfoodfacts', function (Container $app) {
return new OpenFoodFacts($app);
});
}
}

0 comments on commit 9c71c87

Please sign in to comment.