1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Shared Kernel library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-present LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace LIN3S\SharedKernel\Infrastructure\Symfony\HttpAction; |
15
|
|
|
|
16
|
|
|
use LIN3S\SharedKernel\Application\Event\GetEvents; |
17
|
|
|
use LIN3S\SharedKernel\Application\Event\GetEventsQuery; |
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Beñat Espiña <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ListEventsAction |
25
|
|
|
{ |
26
|
|
|
private const PAGE_SIZE = 25; |
27
|
|
|
private const CACHE_LIFETIME = 60 * 60 * 24 * 365; // 1 year |
28
|
|
|
|
29
|
|
|
private $getEvents; |
30
|
|
|
|
31
|
|
|
public function __construct(GetEvents $getEvents) |
32
|
|
|
{ |
33
|
|
|
$this->getEvents = $getEvents; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __invoke(Request $request) : JsonResponse |
37
|
|
|
{ |
38
|
|
|
$page = $request->query->getInt('page', 1); |
39
|
|
|
$since = $request->query->get('since'); |
40
|
|
|
|
41
|
|
|
$events = $this->getEvents->__invoke(new GetEventsQuery($page, self::PAGE_SIZE, $since)); |
42
|
|
|
|
43
|
|
|
$numberOfEvents = count($events['data']); |
44
|
|
|
$isPageCompleted = self::PAGE_SIZE === $numberOfEvents; |
45
|
|
|
$response = new JsonResponse($events, 0 !== $numberOfEvents ? 200 : 404); |
46
|
|
|
|
47
|
|
|
return $isPageCompleted ? $this->cachedResponse($response) : $response; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function cachedResponse(JsonResponse $response) : JsonResponse |
51
|
|
|
{ |
52
|
|
|
return $response |
53
|
|
|
->setMaxAge(self::CACHE_LIFETIME) |
54
|
|
|
->setSharedMaxAge(self::CACHE_LIFETIME); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|