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

NotifynderManager::__call()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 21
cc 5
eloc 12
nc 4
nop 2
rs 8.7624
1
<?php
2
3
namespace Fenos\Notifynder\Managers;
4
5
use Closure;
6
use BadMethodCallException;
7
use Fenos\Notifynder\Builder\Builder;
8
use Fenos\Notifynder\Contracts\NotifynderManagerContract;
9
use Fenos\Notifynder\Contracts\SenderManagerContract;
10
use Illuminate\Support\Str;
11
12
class NotifynderManager implements NotifynderManagerContract
13
{
14
    /**
15
     * @var Builder
16
     */
17
    protected $builder;
18
19
    protected $sender;
20
21
    public function __construct(SenderManagerContract $sender)
22
    {
23
        $this->sender = $sender;
24
    }
25
26
    public function category($category)
27
    {
28
        $this->builder(true);
29
        $this->builder->category($category);
30
31
        return $this;
32
    }
33
34
    public function loop($data, Closure $callback)
35
    {
36
        $this->builder(true);
37
        $this->builder->loop($data, $callback);
38
39
        return $this;
40
    }
41
42
    public function builder($new = false)
43
    {
44
        if (is_null($this->builder) || $new) {
45
            $this->builder = new Builder();
46
        }
47
48
        return $this->builder;
49
    }
50
51
    public function send()
52
    {
53
        $sent = $this->sender->send($this->builder->getNotifications());
54
        $this->reset();
55
56
        return $sent;
57
    }
58
59
    public function sender()
60
    {
61
        return $this->sender;
62
    }
63
64
    protected function reset()
65
    {
66
        $this->builder = null;
67
    }
68
69
    public function extend($name, Closure $sender)
70
    {
71
        return $this->sender->extend($name, $sender);
72
    }
73
74
    public function __call($name, $arguments)
75
    {
76
        if (Str::startsWith($name, 'send')) {
77
            $sent = $this->sender->sendWithCustomSender($name, $this->builder->getNotifications());
78
            $this->reset();
79
80
            return $sent;
81
        }
82
83
        if ($this->builder instanceof Builder && method_exists($this->builder, $name)) {
84
            $result = call_user_func_array([$this->builder, $name], $arguments);
85
            if (Str::startsWith($name, 'get')) {
86
                return $result;
87
            }
88
89
            return $this;
90
        }
91
92
        $error = "The method [$name] doesn't exist in the class ".self::class;
93
        throw new BadMethodCallException($error);
94
    }
95
}
96