Completed
Push — master ( dfea87...bbf397 )
by Tomáš
04:41
created

ControllerResolver   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 106
ccs 32
cts 35
cp 0.9143
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setControllerClassMap() 0 4 1
A getController() 0 17 3
A getArguments() 0 4 1
A getControllerService() 0 6 1
A decorateControllerWithContainer() 0 8 2
A splitControllerClassAndMethod() 0 12 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2015 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\ControllerAutowire\HttpKernel\Controller;
11
12
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
13
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
17
18
final class ControllerResolver implements ControllerResolverInterface
19
{
20
    /**
21
     * @var ControllerResolverInterface
22
     */
23
    private $controllerResolver;
24
25
    /**
26
     * @var ContainerInterface
27
     */
28
    private $container;
29
30
    /**
31
     * @var ControllerNameParser
32
     */
33
    private $controllerNameParser;
34
35
    /**
36
     * @var string[]
37
     */
38
    private $controllerClassMap;
39
40 9
    public function __construct(
41
        ControllerResolverInterface $controllerResolver,
42
        ContainerInterface $container,
43
        ControllerNameParser $controllerNameParser
44
    ) {
45 9
        $this->controllerResolver = $controllerResolver;
46 9
        $this->container = $container;
47 9
        $this->controllerNameParser = $controllerNameParser;
48 9
    }
49
50 7
    public function setControllerClassMap(array $controllerClassMap)
51
    {
52 7
        $this->controllerClassMap = array_flip($controllerClassMap);
53 7
    }
54
55
    /**
56
     * @return false|array|callable
57
     */
58 8
    public function getController(Request $request)
59
    {
60 8
        if (! $controllerName = $request->attributes->get('_controller')) {
61 1
            return false;
62
        }
63
64 7
        list($class, $method) = $this->splitControllerClassAndMethod($controllerName);
65
66 7
        if (! isset($this->controllerClassMap[$class])) {
67 5
            return $this->controllerResolver->getController($request);
68
        }
69
70 2
        $controller = $this->getControllerService($class);
71 2
        $controller = $this->decorateControllerWithContainer($controller);
72
73 2
        return [$controller, $method];
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function getArguments(Request $request, $controller)
80
    {
81 1
        return $this->controllerResolver->getArguments($request, $controller);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpKe...terface::getArguments() has been deprecated with message: This method is deprecated as of 3.1 and will be removed in 4.0. Please use the {@see ArgumentResolverInterface} instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
    }
83
84
    /**
85
     * @return object
86
     */
87 2
    private function getControllerService(string $class)
88
    {
89 2
        $serviceName = $this->controllerClassMap[$class];
90
91 2
        return $this->container->get($serviceName);
92
    }
93
94
    /**
95
     * @param object $controller
96
     *
97
     * @return object
98
     */
99 2
    private function decorateControllerWithContainer($controller)
100
    {
101 2
        if ($controller instanceof ContainerAwareInterface) {
102 1
            $controller->setContainer($this->container);
103
        }
104
105 2
        return $controller;
106
    }
107
108
    /**
109
     * @return string[]
110
     */
111 7
    private function splitControllerClassAndMethod(string $controllerName) : array
112
    {
113 7
        if (false !== strpos($controllerName, '::')) {
114 3
            return explode('::', $controllerName, 2);
115 4
        } elseif (substr_count($controllerName, ':') === 2) {
116
            $controllerName = $this->controllerNameParser->parse($controllerName);
117
118
            return explode('::', $controllerName, 2);
119 4
        } elseif (false !== strpos($controllerName, ':')) {
120 4
            return explode(':', $controllerName, 2);
121
        }
122
    }
123
}
124