Completed
Push — version-4 ( 511c26...6005c1 )
by
unknown
02:14
created

NotifynderManager::extend()   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
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
rs 10
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
        return $this;
31
    }
32
33
    public function loop($data, Closure $callback)
34
    {
35
        $this->builder(true);
36
        $this->builder->loop($data, $callback);
37
        return $this;
38
    }
39
40
    public function builder($new = false)
41
    {
42
        if(is_null($this->builder) || $new) {
43
            $this->builder = new Builder();
44
        }
45
        return $this->builder;
46
    }
47
    
48
    public function send()
49
    {
50
        $sent = $this->sender->send($this->builder->getNotifications());
51
        $this->reset();
52
        return $sent;
53
    }
54
55
    public function sender()
56
    {
57
        return $this->sender;
58
    }
59
60
    protected function reset()
61
    {
62
        $this->builder = null;
63
    }
64
65
    public function extend($name, Closure $sender)
66
    {
67
        return $this->sender->extend($name, $sender);
68
    }
69
70
    public function __call($name, $arguments)
71
    {
72
        if (Str::startsWith($name, 'send')) {
73
            $sent = $this->sender->sendWithCustomSender($name, $this->builder->getNotifications());
74
            $this->reset();
75
            return $sent;
76
        }
77
78
        if($this->builder instanceof Builder && method_exists($this->builder, $name)) {
79
            $result = call_user_func_array([$this->builder, $name], $arguments);
80
            if(Str::startsWith($name, 'get')) {
81
                return $result;
82
            }
83
            return $this;
84
        }
85
86
        $error = "The method [$name] doesn't exist in the class ".self::class;
87
        throw new BadMethodCallException($error);
88
    }
89
}
90