Completed
Push — version-4 ( 2da0bd...27f202 )
by
unknown
02:09
created

Builder::anonymous()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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 View Code Duplication
    public function category($category)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
27
    {
28
        $categoryId = $category;
29
        if ($category instanceof NotificationCategory) {
30
            $categoryId = $category->getKey();
31
        } elseif (! is_numeric($category)) {
32
            $categoryId = NotificationCategory::byName($category)->firstOrFail()->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 anonymous()
49
    {
50
        $this->setNotificationData('from_type', null);
51
        $this->setNotificationData('from_id', null);
52
53
        return $this;
54
    }
55
56
    public function to()
57
    {
58
        $args = func_get_args();
59
        $this->setEntityData($args, 'to');
60
61
        return $this;
62
    }
63
64
    public function url($url)
65
    {
66
        $this->typeChecker->isString($url);
67
        $this->setNotificationData('url', $url);
68
69
        return $this;
70
    }
71
72
    public function expire($datetime)
73
    {
74
        $this->typeChecker->isDate($datetime);
75
        $this->setNotificationData('expires_at', $datetime);
76
77
        return $this;
78
    }
79
80
    public function extra(array $extra = [])
81
    {
82
        $this->typeChecker->isArray($extra);
83
        $this->setNotificationData('extra', $extra);
84
85
        return $this;
86
    }
87
88
    public function setDates()
89
    {
90
        $date = Carbon::now();
91
92
        $this->setNotificationData('updated_at', $date);
93
        $this->setNotificationData('created_at', $date);
94
    }
95
96
    protected function setEntityData($entity, $property)
97
    {
98
        if (is_array($entity) && count($entity) == 2) {
99
            $this->typeChecker->isString($entity[0]);
100
            $this->typeChecker->isNumeric($entity[1]);
101
102
            $type = $entity[0];
103
            $id = $entity[1];
104
        } elseif ($entity[0] instanceof Model) {
105
            $type = $entity[0]->getMorphClass();
106
            $id = $entity[0]->getKey();
107
        } else {
108
            $this->typeChecker->isNumeric($entity[0]);
109
110
            $type = notifynder_config()->getNotifiedModel();
111
            $id = $entity[0];
112
        }
113
114
        $this->setNotificationData("{$property}_type", $type);
115
        $this->setNotificationData("{$property}_id", $id);
116
    }
117
118
    protected function setNotificationData($key, $value)
119
    {
120
        $this->notification->set($key, $value);
121
    }
122
123
    public function getNotification()
124
    {
125
        if (! $this->notification->isValid()) {
126
            throw new UnvalidNotificationException($this->notification);
127
        }
128
129
        $this->setDates();
130
131
        return $this->notification;
132
    }
133
134
    public function addNotification(Notification $notification)
135
    {
136
        $this->notifications[] = $notification;
137
    }
138
139
    public function getNotifications()
140
    {
141
        if (count($this->notifications) == 0) {
142
            $this->addNotification($this->getNotification());
143
        }
144
145
        return $this->notifications;
146
    }
147
148
    public function loop($data, Closure $callback)
149
    {
150
        $this->typeChecker->isIterable($data);
151
152
        foreach ($data as $key => $value) {
153
            $builder = new static();
154
            $callback($builder, $value, $key);
155
            $this->addNotification($builder->getNotification());
156
        }
157
158
        return $this;
159
    }
160
}
161