ControllerNameResolver::setConverter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Controller;
12
13
use Yarhon\RouteGuardBundle\Exception\InvalidArgumentException;
14
15
/**
16
 * @author Yaroslav Honcharuk <[email protected]>
17
 */
18
class ControllerNameResolver implements ControllerNameResolverInterface
19
{
20
    /**
21
     * @var ControllerNameConverter
22
     */
23
    private $converter;
24
25 19
    public function setConverter(ControllerNameConverter $converter)
26
    {
27 19
        $this->converter = $converter;
28 19
    }
29
30
    /**
31
     * @see \Symfony\Component\HttpKernel\Controller\ControllerResolver::getController For possible $controller forms
32
     *
33
     * {@inheritdoc}
34
     */
35 26
    public function resolve($controller)
36
    {
37 26
        if (is_array($controller) && isset($controller[0], $controller[1])) {
38 2
            if (is_string($controller[0])) {
39 1
                return $controller[0].'::'.$controller[1];
40
            }
41
42 1
            if (is_object($controller[0])) {
43 1
                return get_class($controller[0]).'::'.$controller[1];
44
            }
45
        }
46
47 24
        if (is_object($controller)) {
48 1
            return get_class($controller).'::__invoke';
49
        }
50
51 23
        if (is_string($controller)) {
52 22
            if (function_exists($controller)) {
53 1
                return null;
54
            }
55
56 21
            if ($this->converter) {
57 19
                $controller = $this->converter->convert($controller);
58
            }
59
60 21
            if (false === strpos($controller, '::')) {
61 2
                return $controller.'::__invoke';
62
            }
63
64 19
            return $controller;
65
        }
66
67 1
        throw new InvalidArgumentException('Unable to resolve controller name, the controller is not callable.');
68
    }
69
}
70