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

CallableResolver::getCallable()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 7
nc 8
nop 1
dl 0
loc 17
rs 8.4444
c 1
b 0
f 0
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
}