diff --git a/composer.json b/composer.json index 4568ed5..780cae1 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "ext-ctype": "*", "ext-iconv": "*", "illuminate/support": "^7.9", + "symfony/cache": "5.0.*", "symfony/console": "5.0.*", "symfony/dotenv": "5.0.*", "symfony/flex": "^1.3.1", diff --git a/composer.lock b/composer.lock index 6348fba..f5997aa 100644 --- a/composer.lock +++ b/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": "6d0b74794bebe7f33633550e9c54acb7", + "content-hash": "f4b5fb227f3c56e949f202aaa1cf8000", "packages": [ { "name": "doctrine/inflector", diff --git a/src/Command/GetRaffleWinnerCommand.php b/src/Command/GetRaffleWinnerCommand.php index d2035b5..524d928 100644 --- a/src/Command/GetRaffleWinnerCommand.php +++ b/src/Command/GetRaffleWinnerCommand.php @@ -2,6 +2,7 @@ namespace App\Command; +use DateInterval; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Symfony\Component\Console\Command\Command; @@ -9,6 +10,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; class GetRaffleWinnerCommand extends Command @@ -20,6 +22,11 @@ class GetRaffleWinnerCommand extends Command */ private $client; + /** + * @var \Symfony\Contracts\Cache\CacheInterface + */ + private $cache; + /** * All of the RSVPs for this event. * @@ -43,11 +50,13 @@ class GetRaffleWinnerCommand extends Command public function __construct( HttpClientInterface $client, + CacheInterface $cache, string $name = null ) { parent::__construct($name); $this->client = $client; + $this->cache = $cache; $this->rsvps = new Collection(); $this->yesRsvps = new Collection(); } @@ -94,18 +103,27 @@ class GetRaffleWinnerCommand extends Command */ private function retrieveRsvps(InputInterface $input): void { - $response = $this->client->request( - 'GET', - vsprintf( - 'https://api.meetup.com/%s/events/%d/rsvps', - [ - 'php-south-wales', - $input->getArgument('event_id'), - ] - ) - ); + $eventId = $input->getArgument('event_id'); + $rsvps = $this->cache->getItem(sprintf('rsvps.%d', $eventId)); - $this->rsvps = new Collection($response->toArray()); + if (!$rsvps->isHit()) { + $response = $this->client->request( + 'GET', + vsprintf( + 'https://api.meetup.com/%s/events/%d/rsvps', + [ + 'php-south-wales', + $eventId, + ] + ) + ); + + $rsvps->expiresAfter(DateInterval::createFromDateString('1 hour')); + $this->rsvps = new Collection($response->toArray()); + $this->cache->save($rsvps->set($this->rsvps)); + } else { + $this->rsvps = $rsvps->get(); + } $this->yesRsvps = $this->rsvps->filter(function (array $rsvp): bool { return $rsvp['response'] == 'yes';