diff --git a/src/Command/GetRaffleWinnerCommand.php b/src/Command/GetRaffleWinnerCommand.php new file mode 100644 index 0000000..d2035b5 --- /dev/null +++ b/src/Command/GetRaffleWinnerCommand.php @@ -0,0 +1,128 @@ +client = $client; + $this->rsvps = new Collection(); + $this->yesRsvps = new Collection(); + } + + protected function configure() + { + $this + ->setDescription('Add a short description for your command') + ->addArgument( + 'event_id', + InputArgument::REQUIRED, + 'The meetup.com event ID' + ); + } + + protected function execute( + InputInterface $input, + OutputInterface $output + ): int { + $io = new SymfonyStyle($input, $output); + + $this->retrieveRsvps($input); + $io->comment(sprintf('%d RSVPs', $this->rsvps->count())); + $io->comment(sprintf('%s \'yes\' RSVPs', $this->yesRsvps->count())); + + $this->pickWinner(); + $io->success( + sprintf('Winner: %s', Arr::get($this->winner, 'member.name')) + ); + + $this->openWinnerPhoto(); + + return 0; + } + + /** + * @param \Symfony\Component\Console\Input\InputInterface $input + * + * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface + */ + 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'), + ] + ) + ); + + $this->rsvps = new Collection($response->toArray()); + + $this->yesRsvps = $this->rsvps->filter(function (array $rsvp): bool { + return $rsvp['response'] == 'yes'; + }); + } + + private function pickWinner(): void + { + $this->winner = $this->yesRsvps->random(1)->first(); + } + + private function openWinnerPhoto(): void + { + if ($photo = Arr::get($this->winner, 'member.photo.photo_link')) { + sleep(3); + exec(sprintf('xdg-open %s', $photo)); + } + } + +} \ No newline at end of file