refactor: remove old code
This commit is contained in:
parent
43a7205c85
commit
ae38fbf2b9
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
interface AttendeeLoader
|
||||
{
|
||||
public function getAttendees(): Collection;
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Collection;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
final class RsvpCollection extends Collection
|
||||
{
|
||||
private const RESPONSE_ATTENDING = 'yes';
|
||||
|
||||
public function excludeEventHosts(): self
|
||||
{
|
||||
return (new self($this->items))->filter(
|
||||
function (array $rsvp): bool {
|
||||
return !$rsvp['member']['event_context']['host'];
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function onlyAttending(): self
|
||||
{
|
||||
return (new self($this->items))->filter(
|
||||
function (array $rsvp): bool {
|
||||
return $rsvp['response'] == self::RESPONSE_ATTENDING;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function getNames(): self
|
||||
{
|
||||
return (new self($this->items))->pluck('member.name')->sort();
|
||||
}
|
||||
}
|
|
@ -1,106 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\UseCase;
|
||||
|
||||
use App\Collection\RsvpCollection;
|
||||
use App\ValueObject\Event;
|
||||
use App\ValueObject\Result;
|
||||
use App\ValueObject\Winner;
|
||||
use DateInterval;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
final class FindTheWinner implements UseCaseInterface
|
||||
{
|
||||
|
||||
private int $eventId;
|
||||
|
||||
private HttpClientInterface $client;
|
||||
|
||||
private CacheInterface $cache;
|
||||
|
||||
private RsvpCollection $yesRsvps;
|
||||
|
||||
public function __construct(
|
||||
HttpClientInterface $client,
|
||||
CacheInterface $cache,
|
||||
int $eventId
|
||||
) {
|
||||
$this->eventId = $eventId;
|
||||
$this->client = $client;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
public function __invoke(): Result
|
||||
{
|
||||
$eventData = $this->retrieveEventData();
|
||||
$rsvps = $this->retrieveRsvps();
|
||||
$winner = $this->pickWinner($rsvps);
|
||||
|
||||
return new Result(
|
||||
Winner::createFromArray($winner),
|
||||
Event::createFromArray($eventData),
|
||||
$rsvps
|
||||
);
|
||||
}
|
||||
|
||||
private function retrieveEventData(): array
|
||||
{
|
||||
$eventData = $this->cache->getItem(sprintf('event.%d', $this->eventId));
|
||||
|
||||
if (!$eventData->isHit()) {
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
sprintf(
|
||||
'https://api.meetup.com/%s/events/%d',
|
||||
'php-south-wales',
|
||||
$this->eventId
|
||||
)
|
||||
);
|
||||
|
||||
$eventData->expiresAfter(
|
||||
DateInterval::createFromDateString('1 hour')
|
||||
);
|
||||
$return = $response->toArray();
|
||||
$this->cache->save($eventData->set($return));
|
||||
|
||||
return $return;
|
||||
} else {
|
||||
return $eventData->get();
|
||||
}
|
||||
}
|
||||
|
||||
private function retrieveRsvps(): RsvpCollection
|
||||
{
|
||||
$rsvps = $this->cache->getItem(sprintf('rsvps.%d', $this->eventId));
|
||||
|
||||
if (!$rsvps->isHit()) {
|
||||
$response = $this->client->request(
|
||||
'GET',
|
||||
sprintf(
|
||||
'https://api.meetup.com/%s/events/%d/rsvps',
|
||||
'php-south-wales',
|
||||
$this->eventId
|
||||
)
|
||||
);
|
||||
|
||||
$filteredRsvps = RsvpCollection::make($response->toArray())
|
||||
->excludeEventHosts()
|
||||
->onlyAttending();
|
||||
|
||||
$rsvps->expiresAfter(DateInterval::createFromDateString('1 hour'));
|
||||
$this->cache->save($rsvps->set($filteredRsvps));
|
||||
|
||||
return $filteredRsvps;
|
||||
} else {
|
||||
return $rsvps->get();
|
||||
}
|
||||
}
|
||||
|
||||
private function pickWinner(RsvpCollection $rsvps): array
|
||||
{
|
||||
return $rsvps->random(1)->first()['member'];
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\UseCase;
|
||||
|
||||
use App\ValueObject\Result;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
interface UseCaseInterface
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
HttpClientInterface $client,
|
||||
CacheInterface $cache,
|
||||
int $eventId
|
||||
);
|
||||
|
||||
public function __invoke(): Result;
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\ValueObject;
|
||||
|
||||
use App\Collection\RsvpCollection;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class Event
|
||||
{
|
||||
private string $name;
|
||||
|
||||
private string $link;
|
||||
|
||||
public static function createFromArray(array $data): self
|
||||
{
|
||||
return new static($data);
|
||||
}
|
||||
|
||||
public function getLink(): string
|
||||
{
|
||||
return rtrim($this->link, '/');
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
protected function __construct(array $data)
|
||||
{
|
||||
[
|
||||
'name' => $name,
|
||||
'link' => $link,
|
||||
] = $data;
|
||||
|
||||
$this->name = $name;
|
||||
$this->link = $link;
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ValueObject;
|
||||
|
||||
use App\Collection\RsvpCollection;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
final class Result
|
||||
{
|
||||
private Collection $rsvps;
|
||||
|
||||
private Event $event;
|
||||
|
||||
private Winner $winner;
|
||||
|
||||
public function __construct(
|
||||
Winner $winner,
|
||||
Event $event,
|
||||
RsvpCollection $rsvps
|
||||
) {
|
||||
$this->winner = $winner;
|
||||
$this->event = $event;
|
||||
$this->rsvps = $rsvps;
|
||||
}
|
||||
|
||||
public function getEvent(): Event
|
||||
{
|
||||
return $this->event;
|
||||
}
|
||||
|
||||
public function getRsvps(): RsvpCollection
|
||||
{
|
||||
return $this->rsvps;
|
||||
}
|
||||
|
||||
public function getWinner(): Winner
|
||||
{
|
||||
return $this->winner;
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\ValueObject;
|
||||
|
||||
final class Winner
|
||||
{
|
||||
|
||||
private string $name;
|
||||
|
||||
private string $photo;
|
||||
|
||||
public static function createFromArray(array $data): self
|
||||
{
|
||||
return new static($data);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getPhoto(): ?string
|
||||
{
|
||||
return $this->photo ?? null;
|
||||
}
|
||||
|
||||
protected function __construct(array $data)
|
||||
{
|
||||
[
|
||||
'name' => $name,
|
||||
'photo' => $photo,
|
||||
] = $data;
|
||||
|
||||
$this->name = $name;
|
||||
$this->photo = $photo['photo_link'];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue