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

DefaultResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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