Passed
Push — develop ( b176bf...b39eef )
by Michele
43s queued 10s
created

CallableResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 27
rs 10
c 1
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B getCallable() 0 17 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zanzara\Support;
6
7
use DI\Container;
8
use DI\DependencyException;
9
use DI\NotFoundException;
10
use InvalidArgumentException;
11
12
/**
13
 * Trait CallableResolver
14
 * @package Zanzara\Listener
15
 * @property Container $container
16
 */
17
trait CallableResolver
18
{
19
20
    /**
21
     * Check and resolve a callable.
22
     * @param $callback
23
     * @return array|callable
24
     * @throws DependencyException
25
     * @throws NotFoundException
26
     */
27
    protected function getCallable($callback)
28
    {
29
        // if is a class definition, resolve it to an instance through the container
30
        if (is_array($callback) && count($callback) === 2 && is_string($callback[0]) && class_exists($callback[0])) {
31
            $callback[0] = $this->container->make($callback[0]);
32
        }
33
34
        // if passing a class, we probably want resolve that and call the __invoke method
35
        if (is_string($callback) && class_exists($callback)) {
36
            $callback = $this->container->make($callback);
37
        }
38
39
        if (!is_callable($callback)) {
40
            throw new InvalidArgumentException('The callback parameter must be a valid callable.');
41
        }
42
43
        return $callback;
44
    }
45
}