From dcf53f8b0fb22ef2255f82f54ebf54cd250eb00e Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 14 Feb 2019 02:31:16 +0000 Subject: [PATCH] Get and merge comments for each event --- src/Command/PickWinnerCommand.php | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/Command/PickWinnerCommand.php b/src/Command/PickWinnerCommand.php index 8dce116..adf07d7 100644 --- a/src/Command/PickWinnerCommand.php +++ b/src/Command/PickWinnerCommand.php @@ -10,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 Tightenco\Collect\Support\Collection; class PickWinnerCommand extends Command { @@ -84,5 +85,66 @@ class PickWinnerCommand extends Command $cache->set($cacheKey, json_decode($response->getBody())->events, 3600); } + + $events = collect($events); + $this->picker->setHosts($events); + $this->picker->setComments($this->allComments($events)); + + var_dump($this->picker->getWinners(1)->first()); + } + + /** + * Get all comments (talks and event) for the events. + * + * @param \Tightenco\Collect\Support\Collection $events + * The retrieved events. + * + * @return \Tightenco\Collect\Support\Collection + * The merged comments. + */ + private function allComments(Collection $events): Collection + { + return $events->map(function (\stdClass $event) { + return $this + ->eventComments($event) + ->merge($this->talkComments($event)) + ; + }); + } + + /** + * Get the event comments. + * + * @param \stdClass $event + * The event. + * + * @return \Tightenco\Collect\Support\Collection + */ + private function eventComments(\stdClass $event): Collection + { + $response = $this->client->get( + $event->comments_uri, + ['query' => ['resultsperpage' => 1000]]) + ->getBody(); + + return collect(json_decode($response)->comments); + } + + /** + * Get the talk comments. + * + * @param \stdClass $event + * The event. + * + * @return \Tightenco\Collect\Support\Collection + */ + private function talkComments(\stdClass $event): Collection + { + $response = $this->client->get( + $event->all_talk_comments_uri, + ['query' => ['resultsperpage' => 1000]] + )->getBody(); + + return collect(json_decode($response)->comments); } }