|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RayRutjes\GetEventStore\Client\Http\Feed; |
|
4
|
|
|
|
|
5
|
|
|
use RayRutjes\GetEventStore\EventRecord; |
|
6
|
|
|
|
|
7
|
|
|
final class EventStreamViaPersistentSubscriptionFeed |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
private $events; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $links = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
private $isHeadOfStream; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array $events |
|
26
|
|
|
* @param array $links |
|
27
|
|
|
* @param bool $isHeadOfStream |
|
28
|
|
|
*/ |
|
29
|
|
View Code Duplication |
public function __construct(array $events, array $links, bool $isHeadOfStream) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$this->validateEvents($events); |
|
32
|
|
|
$this->events = $events; |
|
33
|
|
|
|
|
34
|
|
|
foreach ($links as $link) { |
|
35
|
|
|
$this->validateLink($link); |
|
36
|
|
|
$this->links[$link->getRelation()] = $link; |
|
37
|
|
|
} |
|
38
|
|
|
$this->isHeadOfStream = $isHeadOfStream; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $events |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
private function validateEvents(array $events) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
foreach ($events as $event) { |
|
49
|
|
|
if (!$event instanceof EventRecord) { |
|
50
|
|
|
throw new \InvalidArgumentException(sprintf('Expected EventRecord, got %s', get_class($event))); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $link |
|
57
|
|
|
*/ |
|
58
|
|
View Code Duplication |
private function validateLink($link) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
if (!$link instanceof EventStreamViaPersistentSubscriptionFeedLink) { |
|
61
|
|
|
throw new \InvalidArgumentException('Invalid link type %s.', get_class($link)); |
|
62
|
|
|
} |
|
63
|
|
|
if (isset($this->links[$link->getRelation()])) { |
|
64
|
|
|
throw new \InvalidArgumentException(sprintf('Link relation %s already there.', $link->getRelation())); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public function hasPreviousLink(): bool |
|
72
|
|
|
{ |
|
73
|
|
|
return isset($this->links[EventStreamViaPersistentSubscriptionFeedLink::LINK_PREVIOUS]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return EventStreamViaPersistentSubscriptionFeedLink |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getPreviousLink(): EventStreamViaPersistentSubscriptionFeedLink |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->links[EventStreamViaPersistentSubscriptionFeedLink::LINK_PREVIOUS]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function hasNextLink(): bool |
|
88
|
|
|
{ |
|
89
|
|
|
return isset($this->links[EventStreamViaPersistentSubscriptionFeedLink::LINK_NEXT]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return EventStreamViaPersistentSubscriptionFeedLink |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getNextLink(): EventStreamViaPersistentSubscriptionFeedLink |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->links[EventStreamViaPersistentSubscriptionFeedLink::LINK_NEXT]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public function hasLastLink(): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return isset($this->links[EventStreamViaPersistentSubscriptionFeedLink::LINK_LAST]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return EventStreamViaPersistentSubscriptionFeedLink |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getLastLink(): EventStreamViaPersistentSubscriptionFeedLink |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->links[EventStreamViaPersistentSubscriptionFeedLink::LINK_LAST]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return EventStreamViaPersistentSubscriptionFeedLink |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getAckAllLink(): EventStreamViaPersistentSubscriptionFeedLink |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->links[EventStreamViaPersistentSubscriptionFeedLink::LINK_ACK_ALL]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return EventStreamViaPersistentSubscriptionFeedLink |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getNackAllLink(): EventStreamViaPersistentSubscriptionFeedLink |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->links[EventStreamViaPersistentSubscriptionFeedLink::LINK_NACK_ALL]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getEvents(): array |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->events; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function isHeadOfStream(): bool |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->isHeadOfStream; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.