Builder::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Fenos\Notifynder\Builder;
4
5
use Closure;
6
use ArrayAccess;
7
use Carbon\Carbon;
8
use Illuminate\Database\Eloquent\Model;
9
use Fenos\Notifynder\Helpers\TypeChecker;
10
use Fenos\Notifynder\Models\NotificationCategory;
11
use Fenos\Notifynder\Exceptions\UnvalidNotificationException;
12
13
/**
14
 * Class Builder.
15
 */
16
class Builder implements ArrayAccess
17
{
18
    /**
19
     * @var Notification
20
     */
21
    protected $notification;
22
23
    /**
24
     * @var array
25
     */
26
    protected $notifications = [];
27
28
    /**
29
     * Builder constructor.
30
     */
31
    public function __construct()
32
    {
33
        $this->notification = new Notification();
34
    }
35
36
    /**
37
     * Set the category for this notification.
38
     *
39
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
40
     * @return $this
41
     */
42
    public function category($category)
43
    {
44
        $categoryId = NotificationCategory::getIdByCategory($category);
45
        $this->setNotificationData('category_id', $categoryId);
46
47
        return $this;
48
    }
49
50
    /**
51
     * Set the sender for this notification.
52
     *
53
     * @return $this
54
     */
55
    public function from()
56
    {
57
        $args = func_get_args();
58
        $this->setEntityData($args, 'from');
59
60
        return $this;
61
    }
62
63
    /**
64
     * Set the sender anonymous for this notification.
65
     *
66
     * @return $this
67
     */
68
    public function anonymous()
69
    {
70
        $this->setNotificationData('from_type', null);
71
        $this->setNotificationData('from_id', null);
72
73
        return $this;
74
    }
75
76
    /**
77
     * Set the receiver for this notification.
78
     *
79
     * @return $this
80
     */
81
    public function to()
82
    {
83
        $args = func_get_args();
84
        $this->setEntityData($args, 'to');
85
86
        return $this;
87
    }
88
89
    /**
90
     * Set the url for this notification.
91
     *
92
     * @param string $url
93
     * @return $this
94
     */
95
    public function url($url)
96
    {
97
        TypeChecker::isString($url);
98
        $this->setNotificationData('url', $url);
99
100
        return $this;
101
    }
102
103
    /**
104
     * Set the expire date for this notification.
105
     *
106
     * @param Carbon|\DateTime $datetime
107
     * @return $this
108
     */
109
    public function expire(Carbon $datetime)
110
    {
111
        TypeChecker::isDate($datetime);
112
        $carbon = new Carbon($datetime);
113
        $this->setNotificationData('expires_at', $carbon);
114
115
        return $this;
116
    }
117
118
    /**
119
     * Set the extra values for this notification.
120
     * You can extend the existing extras or override them - important for multiple calls of extra() on one notification.
121
     *
122
     * @param array $extra
123
     * @param bool $override
124
     * @return $this
125
     */
126
    public function extra(array $extra = [], $override = true)
127
    {
128
        TypeChecker::isArray($extra);
129
        if (! $override) {
130
            $extra = array_merge($this->getNotificationData('extra', []), $extra);
131
        }
132
        $this->setNotificationData('extra', $extra);
133
134
        return $this;
135
    }
136
137
    /**
138
     * Set updated_at and created_at fields.
139
     */
140
    public function setDates()
141
    {
142
        $date = Carbon::now();
143
144
        $this->setNotificationData('updated_at', $date);
145
        $this->setNotificationData('created_at', $date);
146
    }
147
148
    /**
149
     * Set a single field value.
150
     *
151
     * @param string $key
152
     * @param mixed $value
153
     * @return $this
154
     */
155
    public function setField($key, $value)
156
    {
157
        $additionalFields = notifynder_config()->getAdditionalFields();
158
        if (in_array($key, $additionalFields)) {
159
            $this->setNotificationData($key, $value);
160
        }
161
162
        return $this;
163
    }
164
165
    /**
166
     * Set polymorphic model values.
167
     *
168
     * @param array $entity
169
     * @param string $property
170
     */
171
    protected function setEntityData($entity, $property)
172
    {
173
        if (is_array($entity) && count($entity) == 2) {
174
            TypeChecker::isString($entity[0]);
175
            TypeChecker::isNumeric($entity[1]);
176
177
            $type = $entity[0];
178
            $id = $entity[1];
179
        } elseif ($entity[0] instanceof Model) {
180
            $type = $entity[0]->getMorphClass();
181
            $id = $entity[0]->getKey();
182
        } else {
183
            TypeChecker::isNumeric($entity[0]);
184
185
            $type = notifynder_config()->getNotifiedModel();
186
            $id = $entity[0];
187
        }
188
189
        $this->setNotificationData("{$property}_type", $type);
190
        $this->setNotificationData("{$property}_id", $id);
191
    }
192
193
    /**
194
     * Get a single value of this notification.
195
     *
196
     * @param string $key
197
     * @param null|mixed $default
198
     * @return mixed
199
     */
200
    protected function getNotificationData($key, $default = null)
201
    {
202
        return $this->notification->get($key, $default);
203
    }
204
205
    /**
206
     * Set a single value of this notification.
207
     *
208
     * @param string $key
209
     * @param mixed $value
210
     */
211
    protected function setNotificationData($key, $value)
212
    {
213
        $this->notification->set($key, $value);
214
    }
215
216
    /**
217
     * Get the current notification.
218
     *
219
     * @return Notification
220
     * @throws UnvalidNotificationException
221
     */
222
    public function getNotification()
223
    {
224
        if (! $this->notification->isValid()) {
225
            throw new UnvalidNotificationException($this->notification);
226
        }
227
228
        $this->setDates();
229
230
        return $this->notification;
231
    }
232
233
    /**
234
     * Add a notification to the notifications array.
235
     *
236
     * @param Notification $notification
237
     */
238
    public function addNotification(Notification $notification)
239
    {
240
        $this->notifications[] = $notification;
241
    }
242
243
    /**
244
     * Get all notifications.
245
     *
246
     * @return array
247
     * @throws UnvalidNotificationException
248
     */
249
    public function getNotifications()
250
    {
251
        if (count($this->notifications) == 0) {
252
            $this->addNotification($this->getNotification());
253
        }
254
255
        return $this->notifications;
256
    }
257
258
    /**
259
     * Loop over data and call the callback with a new Builder instance and the key and value of the iterated data.
260
     *
261
     * @param array|\Traversable $data
262
     * @param Closure $callback
263
     * @return $this
264
     * @throws UnvalidNotificationException
265
     */
266
    public function loop($data, Closure $callback)
267
    {
268
        TypeChecker::isIterable($data);
269
270
        foreach ($data as $key => $value) {
271
            $builder = new static();
272
            $callback($builder, $value, $key);
273
            $this->addNotification($builder->getNotification());
274
        }
275
276
        return $this;
277
    }
278
279
    /**
280
     * @param string $offset
281
     * @return bool
282
     */
283
    public function offsetExists($offset)
284
    {
285
        return $this->notification->offsetExists($offset);
286
    }
287
288
    /**
289
     * @param string $offset
290
     * @return mixed
291
     */
292
    public function offsetGet($offset)
293
    {
294
        return $this->notification->offsetGet($offset);
295
    }
296
297
    /**
298
     * @param string $offset
299
     * @param mixed $value
300
     */
301
    public function offsetSet($offset, $value)
302
    {
303
        $this->notification->offsetSet($offset, $value);
304
    }
305
306
    /**
307
     * @param string $offset
308
     */
309
    public function offsetUnset($offset)
310
    {
311
        $this->notification->offsetUnset($offset);
312
    }
313
}
314