Completed
Push — version-4 ( 4ff97a...26ca56 )
by
unknown
02:16
created

Builder::setNotificationData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
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\Helpers\TypeChecker;
8
use Fenos\Notifynder\Models\NotificationCategory;
9
use Illuminate\Database\Eloquent\Model;
10
11
class Builder
12
{
13
    protected $notification;
14
15
    protected $notifications = [];
16
17
    protected $typeChecker;
18
19
    public function __construct()
20
    {
21
        $this->notification = new Notification();
22
        $this->typeChecker = new TypeChecker();
23
    }
24
25
    public function category($category)
26
    {
27
        $categoryId = $category;
28
        if (! is_numeric($category)) {
29
            $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...
30
        } elseif($category instanceof NotificationCategory) {
31
            $categoryId = $category->getKey();
32
        }
33
34
        $this->setNotificationData('category_id', $categoryId);
35
36
        return $this;
37
    }
38
39
    public function from()
40
    {
41
        $args = func_get_args();
42
        $this->setEntityData($args, 'from');
43
        return $this;
44
    }
45
46
    public function to()
47
    {
48
        $args = func_get_args();
49
        $this->setEntityData($args, 'to');
50
        return $this;
51
    }
52
53
    public function url($url)
54
    {
55
        $this->typeChecker->isString($url);
56
        $this->setNotificationData('url', $url);
57
        return $this;
58
    }
59
60
    public function expire($datetime)
61
    {
62
        $this->typeChecker->isDate($datetime);
63
        $this->setNotificationData('expire_time', $datetime);
64
        return $this;
65
    }
66
67
    public function extra(array $extra = [])
68
    {
69
        $this->typeChecker->isArray($extra);
70
        $this->setNotificationData('extra', $extra);
71
        return $this;
72
    }
73
74
    public function setDates()
75
    {
76
        $date = Carbon::now();
77
78
        $this->setNotificationData('updated_at', $date);
79
        $this->setNotificationData('created_at', $date);
80
    }
81
82
    protected function setEntityData($entity, $property)
83
    {
84
        if (is_array($entity) && count($entity) == 2) {
85
            $this->typeChecker->isString($entity[0]);
86
            $this->typeChecker->isNumeric($entity[1]);
87
88
            $type = $entity[0];
89
            $id = $entity[1];
90
        } elseif ($entity[0] instanceof Model) {
91
            $type = $entity[0]->getMorphClass();
92
            $id = $entity[0]->getKey();
93
        } else {
94
            $this->typeChecker->isNumeric($entity[0]);
95
96
            $type = notifynder_config()->getNotifiedModel();
97
            $id = $entity[0];
98
        }
99
100
        $this->setNotificationData("{$property}_type", $type);
101
        $this->setNotificationData("{$property}_id", $id);
102
    }
103
104
    protected function setNotificationData($key, $value)
105
    {
106
        $this->notification->set($key, $value);
107
    }
108
109
    public function getNotification()
110
    {
111
        $this->setDates();
112
        return $this->notification;
113
    }
114
115
    public function addNotification(Notification $notification)
116
    {
117
        $this->notifications[] = $notification;
118
    }
119
120
    public function getNotifications()
121
    {
122
        if(count($this->notifications) == 0) {
123
            $this->addNotification($this->getNotification());
124
        }
125
        return $this->notifications;
126
    }
127
128
    public function loop($data, Closure $callback)
129
    {
130
        $this->typeChecker->isIterable($data);
131
132
        foreach ($data as $key => $value) {
133
            $builder = new static();
134
            $callback($builder, $value, $key);
135
            $this->addNotification($builder->getNotification());
136
        }
137
138
        return $this;
139
    }
140
}
141