Completed
Push — version-4 ( a26cca...5903bf )
by
unknown
02:38
created

Builder::setEntityData()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 15
nc 3
nop 2
dl 0
loc 21
rs 9.0534
c 1
b 0
f 1
1
<?php
2
3
namespace Fenos\Notifynder\Builder;
4
5
use Closure;
6
use Carbon\Carbon;
7
use Fenos\Notifynder\Exceptions\UnvalidNotificationException;
8
use Fenos\Notifynder\Helpers\TypeChecker;
9
use Fenos\Notifynder\Models\NotificationCategory;
10
use Illuminate\Database\Eloquent\Model;
11
12
class Builder
13
{
14
    protected $notification;
15
16
    protected $notifications = [];
17
18
    protected $typeChecker;
19
20
    public function __construct()
21
    {
22
        $this->notification = new Notification();
23
        $this->typeChecker = new TypeChecker();
24
    }
25
26
    public function category($category)
27
    {
28
        $categoryId = $category;
29
        if ($category instanceof NotificationCategory) {
30
            $categoryId = $category->getKey();
31
        } elseif (! is_numeric($category)) {
32
            $categoryId = NotificationCategory::byName($category)->findOrFail()->getKey();
0 ignored issues
show
Bug introduced by
The method byName() does not exist on Fenos\Notifynder\Models\NotificationCategory. Did you maybe mean scopeByName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
33
        }
34
35
        $this->setNotificationData('category_id', $categoryId);
36
37
        return $this;
38
    }
39
40
    public function from()
41
    {
42
        $args = func_get_args();
43
        $this->setEntityData($args, 'from');
44
45
        return $this;
46
    }
47
48
    public function to()
49
    {
50
        $args = func_get_args();
51
        $this->setEntityData($args, 'to');
52
53
        return $this;
54
    }
55
56
    public function url($url)
57
    {
58
        $this->typeChecker->isString($url);
59
        $this->setNotificationData('url', $url);
60
61
        return $this;
62
    }
63
64
    public function expire($datetime)
65
    {
66
        $this->typeChecker->isDate($datetime);
67
        $this->setNotificationData('expire_time', $datetime);
68
69
        return $this;
70
    }
71
72
    public function extra(array $extra = [])
73
    {
74
        $this->typeChecker->isArray($extra);
75
        $this->setNotificationData('extra', $extra);
76
77
        return $this;
78
    }
79
80
    public function setDates()
81
    {
82
        $date = Carbon::now();
83
84
        $this->setNotificationData('updated_at', $date);
85
        $this->setNotificationData('created_at', $date);
86
    }
87
88
    protected function setEntityData($entity, $property)
89
    {
90
        if (is_array($entity) && count($entity) == 2) {
91
            $this->typeChecker->isString($entity[0]);
92
            $this->typeChecker->isNumeric($entity[1]);
93
94
            $type = $entity[0];
95
            $id = $entity[1];
96
        } elseif ($entity[0] instanceof Model) {
97
            $type = $entity[0]->getMorphClass();
98
            $id = $entity[0]->getKey();
99
        } else {
100
            $this->typeChecker->isNumeric($entity[0]);
101
102
            $type = notifynder_config()->getNotifiedModel();
103
            $id = $entity[0];
104
        }
105
106
        $this->setNotificationData("{$property}_type", $type);
107
        $this->setNotificationData("{$property}_id", $id);
108
    }
109
110
    protected function setNotificationData($key, $value)
111
    {
112
        $this->notification->set($key, $value);
113
    }
114
115
    public function getNotification()
116
    {
117
        if (! $this->notification->isValid()) {
118
            throw new UnvalidNotificationException($this->notification);
119
        }
120
121
        $this->setDates();
122
123
        return $this->notification;
124
    }
125
126
    public function addNotification(Notification $notification)
127
    {
128
        $this->notifications[] = $notification;
129
    }
130
131
    public function getNotifications()
132
    {
133
        if (count($this->notifications) == 0) {
134
            $this->addNotification($this->getNotification());
135
        }
136
137
        return $this->notifications;
138
    }
139
140
    public function loop($data, Closure $callback)
141
    {
142
        $this->typeChecker->isIterable($data);
143
144
        foreach ($data as $key => $value) {
145
            $builder = new static();
146
            $callback($builder, $value, $key);
147
            $this->addNotification($builder->getNotification());
148
        }
149
150
        return $this;
151
    }
152
}
153