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

SenderManager::hasSender()   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 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Fenos\Notifynder\Managers;
4
5
use Closure;
6
use BadFunctionCallException;
7
use BadMethodCallException;
8
use Fenos\Notifynder\Contracts\SenderContract;
9
use Fenos\Notifynder\Contracts\SenderManagerContract;
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\Str;
12
13
class SenderManager implements SenderManagerContract
14
{
15
    protected $senders = [];
16
17
    public function send(array $notifications)
18
    {
19
        if (count($notifications) == 1) {
20
            return $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...
21
        }
22
23
        return $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...
24
    }
25
26
    public function hasSender($name)
27
    {
28
        return Arr::has($this->senders, $name);
29
    }
30
31
    public function getSender($name)
32
    {
33
        return Arr::get($this->senders, $name);
34
    }
35
36
    public function extend($name, Closure $sender)
37
    {
38
        if (Str::startsWith($name, 'send')) {
39
            $this->senders[$name] = $sender;
40
41
            return true;
42
        }
43
44
        return false;
45
    }
46
47
    public function sendWithCustomSender($name, array $notifications)
48
    {
49
        if ($this->hasSender($name)) {
50
            $sender = call_user_func_array($this->getSender($name), [$notifications]);
51
            if ($sender instanceof SenderContract) {
52
                return (bool) $sender->send($this);
53
            }
54
            throw new BadFunctionCallException("The sender [{$name}] hasn't returned an instance of ".SenderContract::class);
55
        }
56
        throw new BadMethodCallException("The sender [{$name}] isn't registered.");
57
    }
58
59
    public function __call($name, $arguments)
60
    {
61
        if (isset($arguments[0]) && is_array($arguments[0])) {
62
            return $this->sendWithCustomSender($name, $arguments[0]);
63
        }
64
65
        throw new BadMethodCallException('No argument passed to the custom sender, please provide notifications array');
66
    }
67
}
68