|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace SirSova\Webhooks\Subscribers; |
|
5
|
|
|
|
|
6
|
|
|
use DateTimeInterface; |
|
7
|
|
|
use SirSova\Webhooks\Contracts\Subscriber as SubscriberContract; |
|
8
|
|
|
|
|
9
|
|
|
class DatabaseSubscriber implements SubscriberContract |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var int |
|
13
|
|
|
*/ |
|
14
|
|
|
private $id; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var SubscriberContract |
|
17
|
|
|
*/ |
|
18
|
|
|
private $subscriber; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var DateTimeInterface|null |
|
21
|
|
|
*/ |
|
22
|
|
|
private $createdAt; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var DateTimeInterface|null |
|
25
|
|
|
*/ |
|
26
|
|
|
private $updatedAt; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct( |
|
29
|
|
|
SubscriberContract $subscriber, |
|
30
|
|
|
int $id, |
|
31
|
|
|
?DateTimeInterface $createdAt = null, |
|
32
|
|
|
?DateTimeInterface $updatedAt = null |
|
33
|
|
|
) { |
|
34
|
|
|
$this->id = $id; |
|
35
|
|
|
$this->subscriber = $subscriber; |
|
36
|
|
|
$this->createdAt = $createdAt; |
|
37
|
|
|
$this->updatedAt = $updatedAt; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param int $id |
|
42
|
|
|
* @param string $event |
|
43
|
|
|
* @param string $url |
|
44
|
|
|
* @param bool $enabled |
|
45
|
|
|
* @param DateTimeInterface|null $createdAt |
|
46
|
|
|
* @param DateTimeInterface|null $updatedAt |
|
47
|
|
|
* |
|
48
|
|
|
* @return DatabaseSubscriber |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function create( |
|
51
|
|
|
int $id, |
|
52
|
|
|
string $event, |
|
53
|
|
|
string $url, |
|
54
|
|
|
bool $enabled = true, |
|
55
|
|
|
?DateTimeInterface $createdAt = null, |
|
56
|
|
|
?DateTimeInterface $updatedAt = null |
|
57
|
|
|
): self { |
|
58
|
|
|
return new static (new Subscriber($event, $url, $enabled), $id, $createdAt, $updatedAt); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return int |
|
63
|
|
|
*/ |
|
64
|
|
|
public function id(): int |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->id; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function event(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->subscriber->event(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function url(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->subscriber->url(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
public function isEnabled(): bool |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->subscriber->isEnabled(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return DateTimeInterface|null |
|
95
|
|
|
*/ |
|
96
|
|
|
public function createdAt(): ?DateTimeInterface |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->createdAt; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return DateTimeInterface|null |
|
103
|
|
|
*/ |
|
104
|
|
|
public function updatedAt(): ?DateTimeInterface |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->updatedAt; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function toArray(): array |
|
113
|
|
|
{ |
|
114
|
|
|
return [ |
|
115
|
|
|
'id' => $this->id(), |
|
116
|
|
|
'event' => $this->event(), |
|
117
|
|
|
'url' => $this->url(), |
|
118
|
|
|
'enabled' => $this->isEnabled(), |
|
119
|
|
|
'created_at' => $this->createdAt(), |
|
120
|
|
|
'updated_at' => $this->updatedAt() |
|
121
|
|
|
]; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|