CallableResolver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

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

1 Method

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