Passed
Branch master (1ac090)
by Hong
02:12
created

DefaultResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 32
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolve() 0 12 3
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Route
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Route\Resolver;
13
14
/**
15
 * DefaultResolver
16
 *
17
 * Resolving [controllerName, methodName]into a request handler callable
18
 *
19
 * @package Phoole\Route
20
 */
21
class DefaultResolver implements ResolverInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $namespace;
27
28
    /**
29
     * set the namespace to search thru
30
     */
31
    public function __construct(string $namespace = '')
32
    {
33
        $this->namespace = $namespace;
34
    }
35
    
36
    /**
37
     * Resolve [controllerName, methodName] to a callable
38
     *
39
     * {@inheritDoc}
40
     */
41
    public function resolve($notCallable): callable
42
    {
43
        try {
44
            $controllerName = $this->namespace . '\\' . $notCallable[0];
45
            $methodName = $notCallable[1];
46
            $result = [new $controllerName(), $methodName];
47
            if (is_callable($result)) {
48
                return $result;
49
            }
50
            throw new \Exception("unable to resolve " . $notCallable[0]);
51
        } catch (\Throwable $e) {
52
            throw new \InvalidArgumentException($e->getMessage());
53
        }
54
    }
55
}
56