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

SenderManager::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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