1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SirSova\Webhooks\Repositories; |
5
|
|
|
|
6
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidatorFactory; |
7
|
|
|
use Illuminate\Contracts\Validation\Validator; |
8
|
|
|
use Illuminate\Database\ConnectionInterface; |
9
|
|
|
use Illuminate\Database\Query\Builder; |
10
|
|
|
use Illuminate\Validation\ValidationException; |
11
|
|
|
use SirSova\Webhooks\Contracts\SubscriberRepository; |
12
|
|
|
use SirSova\Webhooks\Exceptions\UnableToSaveException; |
13
|
|
|
use SirSova\Webhooks\Subscribers\DatabaseSubscriber; |
14
|
|
|
use SirSova\Webhooks\Subscribers\SubscribersCollection; |
15
|
|
|
|
16
|
|
|
class DatabaseSubscriberRepository implements SubscriberRepository |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ConnectionInterface |
21
|
|
|
*/ |
22
|
|
|
private $connection; |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $table; |
27
|
|
|
/** |
28
|
|
|
* @var ValidatorFactory |
29
|
|
|
*/ |
30
|
|
|
private $validatorFactory; |
31
|
|
|
|
32
|
|
|
public function __construct(ConnectionInterface $connection, ValidatorFactory $validatorFactory, string $table) |
33
|
|
|
{ |
34
|
|
|
$this->connection = $connection; |
35
|
|
|
$this->table = $table; |
36
|
|
|
$this->validatorFactory = $validatorFactory; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $id |
41
|
|
|
* |
42
|
|
|
* @return DatabaseSubscriber|null |
43
|
|
|
*/ |
44
|
|
|
public function find($id): ?DatabaseSubscriber |
45
|
|
|
{ |
46
|
|
|
$object = $this->query()->where('id', '=', $id)->first(); |
47
|
|
|
|
48
|
|
|
return $object === null ? null : $this->createSubscriber($object); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $event |
53
|
|
|
* @param bool $enabledOnly |
54
|
|
|
* |
55
|
|
|
* @return SubscribersCollection |
56
|
|
|
*/ |
57
|
|
|
public function findAllByEvent(string $event, bool $enabledOnly = false): SubscribersCollection |
58
|
|
|
{ |
59
|
|
|
$query = $this->query()->where('event', '=', $event); |
60
|
|
|
|
61
|
|
|
if ($enabledOnly) { |
62
|
|
|
$query->where('enabled', '=', true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$result = $query->get()->map(function ($object) { |
66
|
|
|
return $this->createSubscriber($object); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
return new SubscribersCollection($result); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $data |
74
|
|
|
* |
75
|
|
|
* @return DatabaseSubscriber |
76
|
|
|
* @throws ValidationException |
77
|
|
|
*/ |
78
|
|
|
public function create($data): DatabaseSubscriber |
79
|
|
|
{ |
80
|
|
|
$validator = $this->makeCreationValidator($data); |
81
|
|
|
|
82
|
|
|
if ($validator->fails()) { |
83
|
|
|
throw new ValidationException($validator); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$id = $this->query()->insertGetId($data); |
87
|
|
|
$subscriber = $this->find($id); |
88
|
|
|
|
89
|
|
|
if ($subscriber !== null) { |
90
|
|
|
return $subscriber; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
throw new UnableToSaveException('Something going wrong, subscriber was not saved to Database'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $id |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function remove($id): bool |
102
|
|
|
{ |
103
|
|
|
return (bool)$this->query()->delete($id); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param object|array $object |
108
|
|
|
* |
109
|
|
|
* @return DatabaseSubscriber |
110
|
|
|
*/ |
111
|
|
|
private function createSubscriber($object): DatabaseSubscriber |
112
|
|
|
{ |
113
|
|
|
return DatabaseSubscriber::create($object->id, $object->event, $object->url, $object->enabled, $object->created_at, $object->updated_at); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return Builder |
118
|
|
|
*/ |
119
|
|
|
private function query(): Builder |
120
|
|
|
{ |
121
|
|
|
return $this->connection->table($this->table); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array $data |
126
|
|
|
* |
127
|
|
|
* @return Validator |
128
|
|
|
*/ |
129
|
|
|
private function makeCreationValidator(array $data): Validator |
130
|
|
|
{ |
131
|
|
|
return $this->validatorFactory->make($data, [ |
132
|
|
|
'event' => 'required|string', |
133
|
|
|
'url' => 'required|url', |
134
|
|
|
'enabled' => 'boolean' |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|