NotifynderManager   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 121
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A category() 0 7 1
A loop() 0 7 1
A builder() 0 8 3
A send() 0 7 1
A sender() 0 4 1
A reset() 0 4 1
A extend() 0 4 1
B __call() 0 21 5
1
<?php
2
3
namespace Fenos\Notifynder\Managers;
4
5
use Closure;
6
use BadMethodCallException;
7
use Illuminate\Support\Str;
8
use Fenos\Notifynder\Builder\Builder;
9
use Fenos\Notifynder\Contracts\SenderManagerContract;
10
use Fenos\Notifynder\Contracts\NotifynderManagerContract;
11
12
/**
13
 * Class NotifynderManager.
14
 */
15
class NotifynderManager implements NotifynderManagerContract
16
{
17
    /**
18
     * @var Builder
19
     */
20
    protected $builder;
21
22
    /**
23
     * @var SenderManagerContract
24
     */
25
    protected $sender;
26
27
    /**
28
     * NotifynderManager constructor.
29
     * @param SenderManagerContract $sender
30
     */
31
    public function __construct(SenderManagerContract $sender)
32
    {
33
        $this->sender = $sender;
34
    }
35
36
    /**
37
     * @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
38
     * @return $this
39
     */
40
    public function category($category)
41
    {
42
        $this->builder(true);
43
        $this->builder->category($category);
44
45
        return $this;
46
    }
47
48
    /**
49
     * @param array|\Traversable $data
50
     * @param Closure $callback
51
     * @return $this
52
     */
53
    public function loop($data, Closure $callback)
54
    {
55
        $this->builder(true);
56
        $this->builder->loop($data, $callback);
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param bool $force
63
     * @return Builder
64
     */
65
    public function builder($force = false)
66
    {
67
        if (is_null($this->builder) || $force) {
68
            $this->builder = new Builder();
69
        }
70
71
        return $this->builder;
72
    }
73
74
    /**
75
     * @return bool
76
     */
77
    public function send()
78
    {
79
        $sent = $this->sender->send($this->builder->getNotifications());
80
        $this->reset();
81
82
        return (bool) $sent;
83
    }
84
85
    /**
86
     * @return SenderManagerContract
87
     */
88
    public function sender()
89
    {
90
        return $this->sender;
91
    }
92
93
    protected function reset()
94
    {
95
        $this->builder = null;
96
    }
97
98
    /**
99
     * @param string $name
100
     * @param Closure $sender
101
     * @return bool
102
     */
103
    public function extend($name, Closure $sender)
104
    {
105
        return (bool) $this->sender->extend($name, $sender);
106
    }
107
108
    /**
109
     * @param $name
110
     * @param $arguments
111
     * @return $this|bool
112
     * @throws BadMethodCallException
113
     */
114
    public function __call($name, $arguments)
115
    {
116
        if (Str::startsWith($name, 'send')) {
117
            $sent = $this->sender->sendWithCustomSender($name, $this->builder->getNotifications());
118
            $this->reset();
119
120
            return (bool) $sent;
121
        }
122
123
        if ($this->builder instanceof Builder && method_exists($this->builder, $name)) {
124
            $result = call_user_func_array([$this->builder, $name], $arguments);
125
            if (Str::startsWith($name, 'get')) {
126
                return $result;
127
            }
128
129
            return $this;
130
        }
131
132
        $error = "The method [$name] doesn't exist in the class ".self::class;
133
        throw new BadMethodCallException($error);
134
    }
135
}
136