ControllerNameResolver::resolve()   B
last analyzed

Complexity

Conditions 10
Paths 16

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 33
ccs 17
cts 17
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 16
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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