|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Simple EventStore Manager package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace SimpleEventStoreManager\Infrastructure\DataTransformers; |
|
12
|
|
|
|
|
13
|
|
|
use SimpleEventStoreManager\Infrastructure\DataTransformers\Contracts\DataTransformerInterface; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use JMS\Serializer\Serializer; |
|
18
|
|
|
|
|
19
|
|
|
class YamlEventDataTransformer extends AbstractEventDataTransformer implements DataTransformerInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param array $events |
|
23
|
|
|
* @param int $eventsCount |
|
24
|
|
|
* @param int $page |
|
25
|
|
|
* @param int $maxPerPage |
|
26
|
|
|
* |
|
27
|
|
|
* @return Response |
|
28
|
|
|
*/ |
|
29
|
|
|
public function transform($events, $eventsCount, $page, $maxPerPage) |
|
30
|
|
|
{ |
|
31
|
|
|
$pageCount = count($events); |
|
32
|
|
|
$response = new Response( |
|
33
|
|
|
$this->serializer->serialize( |
|
34
|
|
|
[ |
|
35
|
|
|
'_meta' => [ |
|
36
|
|
|
'page' => $page, |
|
37
|
|
|
'records_per_page' => $maxPerPage, |
|
38
|
|
|
'total_pages' => $numberOfPages = ceil($eventsCount/$maxPerPage), |
|
39
|
|
|
'total_count' => $eventsCount |
|
40
|
|
|
], |
|
41
|
|
|
'_links' => [ |
|
42
|
|
|
$this->calculateLinks($page, $numberOfPages) |
|
|
|
|
|
|
43
|
|
|
], |
|
44
|
|
|
'events' => (is_array($events)) ? $events : $this->convertEventsDataToArray($events) |
|
45
|
|
|
], 'yml'), |
|
46
|
|
|
$this->getHttpStatusCode($pageCount) |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
// set infinite cache if page is complete |
|
50
|
|
View Code Duplication |
if ($maxPerPage === $pageCount) { |
|
|
|
|
|
|
51
|
|
|
$response |
|
52
|
|
|
->setMaxAge(60 * 60 * 24 * 365) |
|
53
|
|
|
->setSharedMaxAge(60 * 60 * 24 * 365); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$response->headers->set('Content-type', 'text/yaml'); |
|
57
|
|
|
$response->setCharset('utf-8'); |
|
58
|
|
|
|
|
59
|
|
|
return $response; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|