Completed
Push — master ( 870291...b9fe13 )
by Tomáš
03:25
created

splitControllerClassAndMethod()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.5923

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 9
cp 0.6667
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 1
crap 4.5923
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