1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Builder; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Closure; |
8
|
|
|
use Fenos\Notifynder\Exceptions\UnvalidNotificationException; |
9
|
|
|
use Fenos\Notifynder\Helpers\TypeChecker; |
10
|
|
|
use Fenos\Notifynder\Models\NotificationCategory; |
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
12
|
|
|
|
13
|
|
|
class Builder implements ArrayAccess |
14
|
|
|
{ |
15
|
|
|
protected $notification; |
16
|
|
|
|
17
|
|
|
protected $notifications = []; |
18
|
|
|
|
19
|
|
|
protected $typeChecker; |
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->notification = new Notification(); |
24
|
|
|
$this->typeChecker = new TypeChecker(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function category($category) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$categoryId = $category; |
30
|
|
|
if ($category instanceof NotificationCategory) { |
31
|
|
|
$categoryId = $category->getKey(); |
32
|
|
|
} elseif (!is_numeric($category)) { |
33
|
|
|
$categoryId = NotificationCategory::byName($category)->firstOrFail()->getKey(); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->setNotificationData('category_id', $categoryId); |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function from() |
42
|
|
|
{ |
43
|
|
|
$args = func_get_args(); |
44
|
|
|
$this->setEntityData($args, 'from'); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function anonymous() |
50
|
|
|
{ |
51
|
|
|
$this->setNotificationData('from_type', null); |
52
|
|
|
$this->setNotificationData('from_id', null); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function to() |
58
|
|
|
{ |
59
|
|
|
$args = func_get_args(); |
60
|
|
|
$this->setEntityData($args, 'to'); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function url($url) |
66
|
|
|
{ |
67
|
|
|
$this->typeChecker->isString($url); |
68
|
|
|
$this->setNotificationData('url', $url); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function expire($datetime) |
74
|
|
|
{ |
75
|
|
|
$this->typeChecker->isDate($datetime); |
76
|
|
|
$this->setNotificationData('expires_at', $datetime); |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function extra(array $extra = []) |
82
|
|
|
{ |
83
|
|
|
$this->typeChecker->isArray($extra); |
84
|
|
|
$this->setNotificationData('extra', $extra); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function setDates() |
90
|
|
|
{ |
91
|
|
|
$date = Carbon::now(); |
92
|
|
|
|
93
|
|
|
$this->setNotificationData('updated_at', $date); |
94
|
|
|
$this->setNotificationData('created_at', $date); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setField($key, $value) |
98
|
|
|
{ |
99
|
|
|
$additionalFields = notifynder_config()->getAdditionalFields(); |
100
|
|
|
if (in_array($key, $additionalFields)) { |
101
|
|
|
$this->setNotificationData($key, $value); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function setEntityData($entity, $property) |
108
|
|
|
{ |
109
|
|
|
if (is_array($entity) && count($entity) == 2) { |
110
|
|
|
$this->typeChecker->isString($entity[0]); |
111
|
|
|
$this->typeChecker->isNumeric($entity[1]); |
112
|
|
|
|
113
|
|
|
$type = $entity[0]; |
114
|
|
|
$id = $entity[1]; |
115
|
|
|
} elseif ($entity[0] instanceof Model) { |
116
|
|
|
$type = $entity[0]->getMorphClass(); |
117
|
|
|
$id = $entity[0]->getKey(); |
118
|
|
|
} else { |
119
|
|
|
$this->typeChecker->isNumeric($entity[0]); |
120
|
|
|
|
121
|
|
|
$type = notifynder_config()->getNotifiedModel(); |
122
|
|
|
$id = $entity[0]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->setNotificationData("{$property}_type", $type); |
126
|
|
|
$this->setNotificationData("{$property}_id", $id); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function setNotificationData($key, $value) |
130
|
|
|
{ |
131
|
|
|
$this->notification->set($key, $value); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getNotification() |
135
|
|
|
{ |
136
|
|
|
if (!$this->notification->isValid()) { |
137
|
|
|
throw new UnvalidNotificationException($this->notification); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->setDates(); |
141
|
|
|
|
142
|
|
|
return $this->notification; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function addNotification(Notification $notification) |
146
|
|
|
{ |
147
|
|
|
$this->notifications[] = $notification; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getNotifications() |
151
|
|
|
{ |
152
|
|
|
if (count($this->notifications) == 0) { |
153
|
|
|
$this->addNotification($this->getNotification()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $this->notifications; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function loop($data, Closure $callback) |
160
|
|
|
{ |
161
|
|
|
$this->typeChecker->isIterable($data); |
162
|
|
|
|
163
|
|
|
foreach ($data as $key => $value) { |
164
|
|
|
$builder = new static(); |
165
|
|
|
$callback($builder, $value, $key); |
166
|
|
|
$this->addNotification($builder->getNotification()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function offsetExists($offset) |
173
|
|
|
{ |
174
|
|
|
return $this->notification->offsetExists($offset); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function offsetGet($offset) |
178
|
|
|
{ |
179
|
|
|
return $this->notification->offsetGet($offset); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function offsetSet($offset, $value) |
183
|
|
|
{ |
184
|
|
|
$this->notification->offsetSet($offset, $value); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function offsetUnset($offset) |
188
|
|
|
{ |
189
|
|
|
$this->notification->offsetUnset($offset); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
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.