Completed
Push — master ( 2f25c9...4c0020 )
by
unknown
06:10
created

SenderManager::hasSender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
     * @param array $notifications
25
     * @return bool
26
     */
27
    public function send(array $notifications)
28
    {
29
        if (count($notifications) == 1) {
30
            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...
31
        }
32
33
        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...
34
    }
35
36
    /**
37
     * @param string $name
38
     * @return bool
39
     */
40
    public function hasSender($name)
41
    {
42
        return Arr::has($this->senders, $name);
43
    }
44
45
    /**
46
     * @param string $name
47
     * @return Closure
48
     */
49
    public function getSender($name)
50
    {
51
        return Arr::get($this->senders, $name);
52
    }
53
54
    /**
55
     * @param string $name
56
     * @param Closure $sender
57
     * @return bool
58
     */
59
    public function extend($name, Closure $sender)
60
    {
61
        if (Str::startsWith($name, 'send')) {
62
            $this->senders[$name] = $sender;
63
64
            return true;
65
        }
66
67
        return false;
68
    }
69
70
    /**
71
     * @param string $name
72
     * @param array $notifications
73
     * @return bool
74
     * @throws BadFunctionCallException
75
     * @throws BadMethodCallException
76
     */
77
    public function sendWithCustomSender($name, array $notifications)
78
    {
79
        if ($this->hasSender($name)) {
80
            $sender = call_user_func_array($this->getSender($name), [$notifications]);
81
            if ($sender instanceof SenderContract) {
82
                return (bool) $sender->send($this);
83
            }
84
            throw new BadFunctionCallException("The sender [{$name}] hasn't returned an instance of ".SenderContract::class);
85
        }
86
        throw new BadMethodCallException("The sender [{$name}] isn't registered.");
87
    }
88
89
    /**
90
     * @param string $name
91
     * @param array $arguments
92
     * @return bool
93
     * @throws BadMethodCallException
94
     */
95
    public function __call($name, array $arguments)
96
    {
97
        if (array_key_exists(0, $arguments) && isset($arguments[0]) && is_array($arguments[0])) {
98
            return $this->sendWithCustomSender($name, $arguments[0]);
99
        }
100
101
        throw new BadMethodCallException('No argument passed to the custom sender, please provide notifications array');
102
    }
103
}
104