diff --git a/lumen/app/Console/Commands/ImportEvents.php b/lumen/app/Console/Commands/ImportEvents.php index 566395a..9c1c6c8 100644 --- a/lumen/app/Console/Commands/ImportEvents.php +++ b/lumen/app/Console/Commands/ImportEvents.php @@ -4,7 +4,8 @@ use Illuminate\Console\Command; use App\Importer\Contracts\EventDataProvider; -use MongoDB\Client as MongoClient; +use Illuminate\Support\Facades\Queue; +use \App\Jobs\ImportEvent; class ImportEvents extends Command { @@ -21,11 +22,20 @@ public function __construct(EventDataProvider $eventDataProvider) { public function handle() { $location = $this->argument('location'); + $numberOfItems = 0; - $events = $this->eventDataProvider->getByLocation($location); - - foreach ($events as $event) { - dispatch(new \App\Jobs\ImportEvent($event)); - } + $pagination = $this->eventDataProvider->getByLocationPagination($location); + + $page = 1; + do { + $events = $this->eventDataProvider->getByLocationPage($page, $location); + foreach ($events as $event) { + Queue::push(new ImportEvent($event)); + } + $numberOfItems = count($events); + $page++; + } while($pagination->page_count > $page); + + $this->line("Imported Events: $numberOfItems"); } } \ No newline at end of file diff --git a/lumen/app/Http/Controllers/EventsController.php b/lumen/app/Http/Controllers/EventsController.php new file mode 100644 index 0000000..ef96b25 --- /dev/null +++ b/lumen/app/Http/Controllers/EventsController.php @@ -0,0 +1,39 @@ +client = $mongoClient; + } + + public function fetch(Request $request) { + $page = intval($request->get('page', 1)); + $size = intval($request->get('size', 10)); + + $eventStorage = $this->client->test->events; + + $cursor = $eventStorage->find([], [ + 'skip' => (($page - 1) * $size), + 'limit' => $size + ]); + + $total = $eventStorage->countDocuments(); + $lastPage = ceil($total/$size); + $items = $cursor->toArray(); + return response()->json([ + 'total' => $total, + 'last_page' => $lastPage, + 'current_page' => $page, + 'size' => $size, + 'items' => $items + ]); + } +} \ No newline at end of file diff --git a/lumen/app/Importer/Contracts/EventDataProvider.php b/lumen/app/Importer/Contracts/EventDataProvider.php index 8d8fec6..940ff87 100644 --- a/lumen/app/Importer/Contracts/EventDataProvider.php +++ b/lumen/app/Importer/Contracts/EventDataProvider.php @@ -4,4 +4,6 @@ interface EventDataProvider { public function getByLocation(string $location); + public function getByLocationPage(int $page, string $location); + public function getByLocationPagination(string $location); } \ No newline at end of file diff --git a/lumen/app/Importer/Contracts/VenueDataProvider.php b/lumen/app/Importer/Contracts/VenueDataProvider.php new file mode 100644 index 0000000..e2347ec --- /dev/null +++ b/lumen/app/Importer/Contracts/VenueDataProvider.php @@ -0,0 +1,7 @@ +client = $client; } - public function getByLocation(string $location) { - $response = $this->client->get('events/search', [ - 'query' => [ - 'location.address' => $location - ] - ]); - + private function getData(array $queryArray){ + $response = $this->client->get('events/search', $queryArray); $responseData = json_decode($response->getBody()->getContents()); return array_map(function ($event) { @@ -30,6 +25,7 @@ public function getByLocation(string $location) { 'url' => data_get($event, 'url'), 'start' => data_get($event, 'start.utc'), 'end' => data_get($event, 'end.utc'), + 'image_url' => data_get($event, 'logo.original.url'), ]; data_set($newEvent, 'metadata.venue_id', data_get($event, 'venue_id')); @@ -37,4 +33,33 @@ public function getByLocation(string $location) { return $newEvent; }, $responseData->events); } + + public function getByLocation(string $location) { + return $this->getData([ + 'query' => [ + 'location.address' => $location + ] + ]); + } + + public function getByLocationPage(int $page, string $location) + { + return $this->getData([ + 'query' => [ + 'page' => $page, + 'location.address' => $location + ] + ]); + } + + public function getByLocationPagination(string $location) + { + $response = $this->client->get('events/search', [ + 'query' => [ + 'location.address' => $location + ] + ]); + $responseData = json_decode($response->getBody()->getContents()); + return $responseData->pagination; + } } \ No newline at end of file diff --git a/lumen/app/Importer/EventBrite/VenueDataProvider.php b/lumen/app/Importer/EventBrite/VenueDataProvider.php new file mode 100644 index 0000000..a7db683 --- /dev/null +++ b/lumen/app/Importer/EventBrite/VenueDataProvider.php @@ -0,0 +1,28 @@ +client = $client; + } + + public function getById(string $id){ + $response = $this->client->get("venues/$id"); + $responseData = json_decode($response->getBody()->getContents()); + + return [ + 'external_id' => data_get($responseData, 'id'), + 'source' => 'eventbrite', + 'name' => data_get($responseData, 'name'), + 'address' => data_get($responseData, 'address.localized_address_display') + ]; + } + +} diff --git a/lumen/app/Jobs/ImportEvent.php b/lumen/app/Jobs/ImportEvent.php index 5a01915..4505d8e 100644 --- a/lumen/app/Jobs/ImportEvent.php +++ b/lumen/app/Jobs/ImportEvent.php @@ -3,12 +3,11 @@ namespace App\Jobs; use MongoDB\Client as MongoClient; -use App\Jobs\ImportEvent; +use Illuminate\Support\Facades\Queue; class ImportEvent extends Job -{ +{ protected $event; - /** * Create a new job instance. * @@ -37,7 +36,7 @@ public function handle(MongoClient $client) if (empty($event)) { $venueStorage = $client->test->venues; $venueId = data_get($toImport, 'metadata.venue_id'); - + $venue = $venueStorage->findOne([ 'external_id' => $venueId ]); @@ -47,8 +46,10 @@ public function handle(MongoClient $client) return $eventStorage->insertOne($toImport); } - dispatch(new ImportVenue($venueId)); - dispatch(new ImportEvent($toImport)); + Queue::bulk([ + new ImportVenue($venueId), + new ImportEvent($toImport) + ]); } } -} +} \ No newline at end of file diff --git a/lumen/app/Jobs/ImportVenue.php b/lumen/app/Jobs/ImportVenue.php index 086dfcc..057c7b3 100644 --- a/lumen/app/Jobs/ImportVenue.php +++ b/lumen/app/Jobs/ImportVenue.php @@ -3,17 +3,17 @@ namespace App\Jobs; use MongoDB\Client as MongoClient; +use App\Importer\Contracts\VenueDataProvider; class ImportVenue extends Job -{ +{ protected $venueId; - /** * Create a new job instance. * * @return void */ - public function __construct($venueId) + public function __construct(string $venueId) { $this->venueId = $venueId; } @@ -23,7 +23,7 @@ public function __construct($venueId) * * @return void */ - public function handle(MongoClient $client) + public function handle(MongoClient $client, VenueDataProvider $dataProvider) { $venueStorage = $client->test->venues; @@ -33,12 +33,9 @@ public function handle(MongoClient $client) if (empty($venue)) { /* TODO: Use VenueDataProvider */ - $venue = [ - 'external_id' => $this->venueId, - 'name' => 'Wizeline' - ]; + $venue = $dataProvider->getById($this->venueId); return $venueStorage->insertOne($venue); } } -} +} \ No newline at end of file diff --git a/lumen/app/Providers/AppServiceProvider.php b/lumen/app/Providers/AppServiceProvider.php index 7af9029..6392c1b 100644 --- a/lumen/app/Providers/AppServiceProvider.php +++ b/lumen/app/Providers/AppServiceProvider.php @@ -25,5 +25,16 @@ public function register() ]); return new \App\Importer\EventBrite\EventDataProvider($client); }); + + $this->app->singleton(\App\Importer\Contracts\VenueDataProvider::class, function () { + $token = config('services.eventbrite.token'); + $client = new \GuzzleHttp\Client([ + 'base_uri' => config('services.eventbrite.base_uri'), + 'headers' => [ + 'Authorization' => "Bearer $token" + ] + ]); + return new \App\Importer\EventBrite\VenueDataProvider($client); + }); } } diff --git a/lumen/bootstrap/app.php b/lumen/bootstrap/app.php index 4c8d41c..0304fdc 100644 --- a/lumen/bootstrap/app.php +++ b/lumen/bootstrap/app.php @@ -23,7 +23,7 @@ realpath(__DIR__.'/../') ); -// $app->withFacades(); +$app->withFacades(); // $app->withEloquent(); @@ -78,6 +78,7 @@ | */ +$app->register(Illuminate\Redis\RedisServiceProvider::class); $app->register(App\Providers\AppServiceProvider::class); // $app->register(App\Providers\AuthServiceProvider::class); // $app->register(App\Providers\EventServiceProvider::class); @@ -94,6 +95,7 @@ */ $app->configure('services'); +$app->make('queue'); $app->router->group([ 'namespace' => 'App\Http\Controllers', diff --git a/lumen/composer.json b/lumen/composer.json index d2fdf35..a76ba48 100644 --- a/lumen/composer.json +++ b/lumen/composer.json @@ -9,7 +9,9 @@ "laravel/lumen-framework": "5.6.*", "vlucas/phpdotenv": "~2.2", "mongodb/mongodb": "^1.4", - "guzzlehttp/guzzle": "^6.3" + "guzzlehttp/guzzle": "^6.3", + "predis/predis": "^1.1", + "illuminate/redis": "^5.6" }, "require-dev": { "fzaninotto/faker": "~1.4", diff --git a/lumen/composer.lock b/lumen/composer.lock index 28308b4..650663e 100644 --- a/lumen/composer.lock +++ b/lumen/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e5c772c34eaab50ff37f901d3e55baf6", + "content-hash": "e12f02c2a0c1a6acd200f5d4e1db83b4", "packages": [ { "name": "doctrine/inflector", @@ -1162,6 +1162,51 @@ "homepage": "https://laravel.com", "time": "2018-08-20T13:25:32+00:00" }, + { + "name": "illuminate/redis", + "version": "v5.6.38", + "source": { + "type": "git", + "url": "https://github.com/illuminate/redis.git", + "reference": "e6c48d60f05849ca5eccd466879390bfef234970" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/redis/zipball/e6c48d60f05849ca5eccd466879390bfef234970", + "reference": "e6c48d60f05849ca5eccd466879390bfef234970", + "shasum": "" + }, + "require": { + "illuminate/contracts": "5.6.*", + "illuminate/support": "5.6.*", + "php": "^7.1.3", + "predis/predis": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.6-dev" + } + }, + "autoload": { + "psr-4": { + "Illuminate\\Redis\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The Illuminate Redis package.", + "homepage": "https://laravel.com", + "time": "2018-07-13T13:28:47+00:00" + }, { "name": "illuminate/session", "version": "v5.6.38", @@ -1740,6 +1785,56 @@ ], "time": "2018-02-13T20:26:39+00:00" }, + { + "name": "predis/predis", + "version": "v1.1.1", + "source": { + "type": "git", + "url": "https://github.com/nrk/predis.git", + "reference": "f0210e38881631afeafb56ab43405a92cafd9fd1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nrk/predis/zipball/f0210e38881631afeafb56ab43405a92cafd9fd1", + "reference": "f0210e38881631afeafb56ab43405a92cafd9fd1", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "require-dev": { + "phpunit/phpunit": "~4.8" + }, + "suggest": { + "ext-curl": "Allows access to Webdis when paired with phpiredis", + "ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol" + }, + "type": "library", + "autoload": { + "psr-4": { + "Predis\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Daniele Alessandri", + "email": "suppakilla@gmail.com", + "homepage": "http://clorophilla.net" + } + ], + "description": "Flexible and feature-complete Redis client for PHP and HHVM", + "homepage": "http://github.com/nrk/predis", + "keywords": [ + "nosql", + "predis", + "redis" + ], + "time": "2016-06-16T16:22:20+00:00" + }, { "name": "psr/container", "version": "1.0.0", @@ -3246,21 +3341,24 @@ }, { "name": "phpunit/php-file-iterator", - "version": "2.0.1", + "version": "2.0.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "cecbc684605bb0cc288828eb5d65d93d5c676d3c" + "reference": "050bedf145a257b1ff02746c31894800e5122946" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/cecbc684605bb0cc288828eb5d65d93d5c676d3c", - "reference": "cecbc684605bb0cc288828eb5d65d93d5c676d3c", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/050bedf145a257b1ff02746c31894800e5122946", + "reference": "050bedf145a257b1ff02746c31894800e5122946", "shasum": "" }, "require": { "php": "^7.1" }, + "require-dev": { + "phpunit/phpunit": "^7.1" + }, "type": "library", "extra": { "branch-alias": { @@ -3289,7 +3387,7 @@ "filesystem", "iterator" ], - "time": "2018-06-11T11:44:00+00:00" + "time": "2018-09-13T20:33:42+00:00" }, { "name": "phpunit/php-text-template", @@ -3432,16 +3530,16 @@ }, { "name": "phpunit/phpunit", - "version": "7.3.4", + "version": "7.3.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "0356331bf62896dc56e3a15030b23b73f38b2935" + "reference": "7b331efabbb628c518c408fdfcaf571156775de2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/0356331bf62896dc56e3a15030b23b73f38b2935", - "reference": "0356331bf62896dc56e3a15030b23b73f38b2935", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/7b331efabbb628c518c408fdfcaf571156775de2", + "reference": "7b331efabbb628c518c408fdfcaf571156775de2", "shasum": "" }, "require": { @@ -3512,7 +3610,7 @@ "testing", "xunit" ], - "time": "2018-09-05T09:58:53+00:00" + "time": "2018-09-08T15:14:29+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", diff --git a/lumen/config/services.php b/lumen/config/services.php index 9b9a07c..64bd943 100644 --- a/lumen/config/services.php +++ b/lumen/config/services.php @@ -1,6 +1,15 @@ [ + 'client' => 'predis', + 'default' => [ + 'host' => env('REDIS_HOST'), + 'password' => null, + 'port' => 6379, + 'database' => 0 + ] + ], 'mongodb' => [ 'host' => env('MONGODB_HOST', '127.0.0.1') ], diff --git a/lumen/routes/web.php b/lumen/routes/web.php index 3be2f2e..9e2bf04 100644 --- a/lumen/routes/web.php +++ b/lumen/routes/web.php @@ -17,14 +17,8 @@ return $router->app->version(); }); -$router->get('/events', function (MongoClient $client) { - $eventStorage = $client->test->events; - $cursor = $eventStorage->find(); - $items = $cursor->toArray(); - - return response()->json($items); -}); +$router->get('/events', 'EventsController@fetch'); -$router->get('/eventbrite', function (EventDataProvider $provider) { +$router->get('/eventbrite', function (EventDataProvider $provider){ return $provider->getByLocation('Guadalajara'); -}); +}); \ No newline at end of file