|
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\Domain\Model\Contracts\EventInterface; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use JMS\Serializer\Serializer; |
|
17
|
|
|
|
|
18
|
|
|
abstract class AbstractEventDataTransformer |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Serializer |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $serializer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Request |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $request; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $paginationLink; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* JsonEventDataTransformer constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param Serializer $serializer |
|
39
|
|
|
* @param Request $request |
|
40
|
|
|
* @param bool $paginationLink |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(Serializer $serializer, Request $request, $paginationLink = false) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->serializer = $serializer; |
|
45
|
|
|
$this->request = $request; |
|
46
|
|
|
$this->paginationLink = $paginationLink; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param $events |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function convertEventsDataToArray($events) |
|
55
|
|
|
{ |
|
56
|
|
|
return array_map( |
|
57
|
|
|
function (EventInterface $event) { |
|
58
|
|
|
return [ |
|
59
|
|
|
'uuid' => $event->uuid(), |
|
60
|
|
|
'version' => $event->version(), |
|
61
|
|
|
'payload' => $event->payload(), |
|
62
|
|
|
'type' => $event->type(), |
|
63
|
|
|
'body' => $event->body(), |
|
64
|
|
|
'occurred_on' => $event->occurredOn(), |
|
65
|
|
|
]; |
|
66
|
|
|
}, |
|
67
|
|
|
$events |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param int $currentPage |
|
73
|
|
|
* @param int $numberOfPages |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function calculateLinks($currentPage, $numberOfPages) |
|
78
|
|
|
{ |
|
79
|
|
|
$baseUrl = $this->getBaseUrl(); |
|
80
|
|
|
$prev = ($currentPage > 1) ? $currentPage - 1 : null; |
|
81
|
|
|
$next = ($currentPage < $numberOfPages) ? $currentPage + 1 : null; |
|
82
|
|
|
|
|
83
|
|
|
$separator = ($this->paginationLink) ? '/' : '?page='; |
|
84
|
|
|
|
|
85
|
|
|
return [ |
|
86
|
|
|
'current' => $baseUrl.$separator.$currentPage, |
|
87
|
|
|
'prev' => ($prev) ? $baseUrl.$separator.$prev : null, |
|
88
|
|
|
'next' => ($next) ? $baseUrl.$separator.$next : null, |
|
89
|
|
|
'last' => ($numberOfPages > 0) ? $baseUrl.$separator.$numberOfPages : null, |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function getBaseUrl() |
|
97
|
|
|
{ |
|
98
|
|
|
return (null !== $page = $this->request->attributes->get('page')) ? str_replace('/'.$page, '', $this->request->getUri()) : str_replace('/?'.$this->request->getQueryString(), '', $this->request->getUri()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param $pageCount |
|
103
|
|
|
* |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getHttpStatusCode($pageCount) |
|
107
|
|
|
{ |
|
108
|
|
|
if ($pageCount <= 0) { |
|
109
|
|
|
return Response::HTTP_NOT_FOUND; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return Response::HTTP_OK; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|