Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish challenge #6

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lumen/app/Console/Commands/ImportEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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");
}
}
39 changes: 39 additions & 0 deletions lumen/app/Http/Controllers/EventsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Http\Controllers;

use MongoDB\Client as MongoClient;
use Illuminate\Http\Request;


class EventsController extends Controller {

private $client;

public function __construct(MongoClient $mongoClient) {
$this->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
]);
}
}
2 changes: 2 additions & 0 deletions lumen/app/Importer/Contracts/EventDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

interface EventDataProvider {
public function getByLocation(string $location);
public function getByLocationPage(int $page, string $location);
public function getByLocationPagination(string $location);
}
7 changes: 7 additions & 0 deletions lumen/app/Importer/Contracts/VenueDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Importer\Contracts;

interface VenueDataProvider {
public function getById(string $id);
}
39 changes: 32 additions & 7 deletions lumen/app/Importer/EventBrite/EventDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ public function __construct(Client $client) {
$this->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) {
Expand All @@ -30,11 +25,41 @@ 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'));

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;
}
}
28 changes: 28 additions & 0 deletions lumen/app/Importer/EventBrite/VenueDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Importer\EventBrite;

use App\Importer\Contracts\VenueDataProvider as DataProvider;
use GuzzleHttp\Client;

class VenueDataProvider implements DataProvider {

protected $client;

public function __construct(Client $client){
$this->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')
];
}

}
15 changes: 8 additions & 7 deletions lumen/app/Jobs/ImportEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
]);
Expand All @@ -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)
]);
}
}
}
}
15 changes: 6 additions & 9 deletions lumen/app/Jobs/ImportVenue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;

Expand All @@ -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);
}
}
}
}
11 changes: 11 additions & 0 deletions lumen/app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
4 changes: 3 additions & 1 deletion lumen/bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
realpath(__DIR__.'/../')
);

// $app->withFacades();
$app->withFacades();

// $app->withEloquent();

Expand Down Expand Up @@ -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);
Expand All @@ -94,6 +95,7 @@
*/

$app->configure('services');
$app->make('queue');

$app->router->group([
'namespace' => 'App\Http\Controllers',
Expand Down
4 changes: 3 additions & 1 deletion lumen/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading