Completed
Push — master ( 0bba58...460685 )
by
unknown
10:36 queued 06:09
created

SenderManager::getCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
rs 10
1
<?php
2
3
namespace Fenos\Notifynder\Managers;
4
5
use Closure;
6
use BadMethodCallException;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Str;
9
use BadFunctionCallException;
10
use Fenos\Notifynder\Contracts\SenderContract;
11
use Fenos\Notifynder\Contracts\SenderManagerContract;
12
13
/**
14
 * Class SenderManager.
15
 */
16
class SenderManager implements SenderManagerContract
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $senders = [];
22
23
    /**
24
     * @var array
25
     */
26
    protected $callbacks = [];
27
28
    /**
29
     * @param array $notifications
30
     * @return bool
31
     */
32
    public function send(array $notifications)
33
    {
34
        if (count($notifications) == 1) {
35
            return (bool) $this->sendSingle($notifications);
0 ignored issues
show
Documentation Bug introduced by
The method sendSingle does not exist on object<Fenos\Notifynder\Managers\SenderManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
36
        }
37
38
        return (bool) $this->sendMultiple($notifications);
0 ignored issues
show
Documentation Bug introduced by
The method sendMultiple does not exist on object<Fenos\Notifynder\Managers\SenderManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
39
    }
40
41
    /**
42
     * @param string $name
43
     * @return bool
44
     */
45
    public function hasSender($name)
46
    {
47
        return Arr::has($this->senders, $name);
48
    }
49
50
    /**
51
     * @param string $name
52
     * @return Closure
53
     */
54
    public function getSender($name)
55
    {
56
        return Arr::get($this->senders, $name);
57
    }
58
59
    /**
60
     * @param string $name
61
     * @param Closure $sender
62
     * @return bool
63
     */
64
    public function extend($name, Closure $sender)
65
    {
66
        if (Str::startsWith($name, 'send')) {
67
            $this->senders[$name] = $sender;
68
69
            return true;
70
        }
71
72
        return false;
73
    }
74
75
    /**
76
     * @param string $class
77
     * @param callable $callback
78
     * @return bool
79
     */
80
    public function setCallback($class, callable $callback)
81
    {
82
        if (class_exists($class)) {
83
            $this->callbacks[$class] = $callback;
84
85
            return true;
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * @param string $class
93
     * @return callable|null
94
     */
95
    public function getCallback($class)
96
    {
97
        return Arr::get($this->callbacks, $class);
98
    }
99
100
    /**
101
     * @param string $name
102
     * @param array $notifications
103
     * @return bool
104
     * @throws BadFunctionCallException
105
     * @throws BadMethodCallException
106
     */
107
    public function sendWithCustomSender($name, array $notifications)
108
    {
109
        if ($this->hasSender($name)) {
110
            $sender = call_user_func_array($this->getSender($name), [$notifications]);
111
            if ($sender instanceof SenderContract) {
112
                return (bool) $sender->send($this);
113
            }
114
            throw new BadFunctionCallException("The sender [{$name}] hasn't returned an instance of ".SenderContract::class);
115
        }
116
        throw new BadMethodCallException("The sender [{$name}] isn't registered.");
117
    }
118
119
    /**
120
     * @param string $name
121
     * @param array $arguments
122
     * @return bool
123
     * @throws BadMethodCallException
124
     */
125
    public function __call($name, array $arguments)
126
    {
127
        if (array_key_exists(0, $arguments) && isset($arguments[0]) && is_array($arguments[0])) {
128
            return $this->sendWithCustomSender($name, $arguments[0]);
129
        }
130
131
        throw new BadMethodCallException('No argument passed to the custom sender, please provide notifications array');
132
    }
133
}
134