Completed
Pull Request — master (#8)
by Tomáš
16:38 queued 08:05
created

ControllerResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2015 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\ControllerAutowire\HttpKernel\Controller;
9
10
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
11
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
15
16
final class ControllerResolver implements ControllerResolverInterface
17
{
18
    /**
19
     * @var ControllerResolverInterface
20
     */
21
    private $controllerResolver;
22
23
    /**
24
     * @var ContainerInterface
25
     */
26
    private $container;
27
28
    /**
29
     * @var ControllerNameParser
30
     */
31
    private $controllerNameParser;
32
33
    /**
34
     * @var string[]
35
     */
36
    private $controllerClassMap;
37
38 7
    public function __construct(
39
        ControllerResolverInterface $controllerResolver,
40
        ContainerInterface $container,
41
        ControllerNameParser $controllerNameParser
42
    ) {
43 7
        $this->controllerResolver = $controllerResolver;
44 7
        $this->container = $container;
45 7
        $this->controllerNameParser = $controllerNameParser;
46 7
    }
47
48 5
    public function setControllerClassMap(array $controllerClassMap)
49
    {
50 5
        $this->controllerClassMap = array_flip($controllerClassMap);
51 5
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 5
    public function getController(Request $request)
57
    {
58 5
        if (!$controllerName = $request->attributes->get('_controller')) {
59
            return false;
60
        }
61
62 5
        list($class, $method) = $this->splitControllerClassAndMethod($controllerName);
63 5
        if (!isset($this->controllerClassMap[$class])) {
64 2
            return $this->controllerResolver->getController($request);
65
        }
66
67 3
        $controller = $this->getControllerService($class);
68 3
        $controller = $this->decorateControllerWithContainer($controller);
69
70 3
        return [$controller, $method];
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1
    public function getArguments(Request $request, $controller)
77
    {
78 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...
79
    }
80
81
    /**
82
     * @return object
83
     */
84 3
    private function getControllerService($class)
85
    {
86 3
        $serviceName = $this->controllerClassMap[$class];
87
88 3
        return $this->container->get($serviceName);
89
    }
90
91
    /**
92
     * @param object $controller
93
     *
94
     * @return object
95
     */
96 3
    private function decorateControllerWithContainer($controller)
97
    {
98 3
        if ($controller instanceof ContainerAwareInterface) {
99 1
            $controller->setContainer($this->container);
100
        }
101
102 3
        return $controller;
103
    }
104
105
    /**
106
     * @return string[]
107
     */
108 5
    private function splitControllerClassAndMethod($controllerName)
109
    {
110 5
        if (false !== strpos($controllerName, '::')) {
111 3
            return explode('::', $controllerName, 2);
112 2
        } elseif (substr_count($controllerName, ':') === 2) {
113 1
            $controllerName = $this->controllerNameParser->parse($controllerName);
114
115 1
            return explode('::', $controllerName, 2);
116 1
        } elseif (false !== strpos($controllerName, ':')) {
117 1
            return explode(':', $controllerName, 2);
118
        }
119
    }
120
}
121