Completed
Branch master (c0d8ef)
by Yaroslav
06:36
created

ControllerNameResolver::resolve()   B

Complexity

Conditions 10
Paths 16

Size

Total Lines 35
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 35
rs 7.6666
c 0
b 0
f 0
ccs 17
cts 17
cp 1
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 2
    public function setConverter(ControllerNameConverter $converter)
26
    {
27 2
        $this->converter = $converter;
28 2
    }
29
30
    /**
31
     * @see \Symfony\Component\HttpKernel\Controller\ControllerResolver::getController For possible $controller forms
32
     *
33
     * {@inheritdoc}
34
     */
35 9
    public function resolve($controller)
36
    {
37 9
        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 7
        if (is_object($controller)) {
48 1
            return get_class($controller).'::__invoke';
49
        }
50
51 6
        if (is_string($controller)) {
52 5
            if (function_exists($controller)) {
53 1
                return null;
54
            }
55
56 4
            if ($this->converter) {
57 2
                $controller = $this->converter->convert($controller);
58
            }
59
60 4
            if (false === strpos($controller, '::')) {
61 2
                return $controller.'::__invoke';
62
            }
63
64
            // TODO: check if controller has exactly two parts here?
65
66 2
            return $controller;
67
        }
68
69 1
        throw new InvalidArgumentException('Unable to resolve controller name, the controller is not callable.');
70
    }
71
}
72