Completed
Push — master ( ad4bdc...54b0a9 )
by Christian
02:47 queued 01:03
created

RestRouteLoader::getControllerLocator()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.3329

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 12
cts 18
cp 0.6667
rs 8.7697
c 0
b 0
f 0
cc 6
nc 13
nop 1
crap 7.3329
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Routing\Loader;
13
14
use FOS\RestBundle\Routing\Loader\Reader\RestControllerReader;
15
use Symfony\Component\Config\FileLocatorInterface;
16
use Symfony\Component\Config\Loader\Loader;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
19
/**
20
 * RestRouteLoader REST-enabled controller router loader.
21
 *
22
 * @author Konstantin Kudryashov <[email protected]>
23
 * @author Bulat Shakirzyanov <[email protected]>
24
 */
25
class RestRouteLoader extends Loader
26
{
27
    protected $container;
28
    protected $controllerReader;
29
    protected $defaultFormat;
30
    protected $locator;
31
32 56
    public function __construct(
33
        ContainerInterface $container,
34
        FileLocatorInterface $locator,
35
        RestControllerReader $controllerReader,
36
        ?string $defaultFormat = 'html'
37
    ) {
38 56
        $this->container = $container;
39 56
        $this->locator = $locator;
40 56
        $this->controllerReader = $controllerReader;
41 56
        $this->defaultFormat = $defaultFormat;
42 56
    }
43
44
    /**
45
     * Returns controller reader.
46
     *
47
     * @return RestControllerReader
48
     */
49 28
    public function getControllerReader()
50
    {
51 28
        return $this->controllerReader;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 39
    public function load($controller, $type = null)
58
    {
59 39
        list($prefix, $class) = $this->getControllerLocator($controller);
60
61 39
        $collection = $this->controllerReader->read(new \ReflectionClass($class));
62 39
        $collection->prependRouteControllersWithPrefix($prefix);
63 39
        $collection->setDefaultFormat($this->defaultFormat);
64
65 39
        return $collection;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 21
    public function supports($resource, $type = null)
72
    {
73 21
        return is_string($resource)
74 21
            && 'rest' === $type
75
            && !in_array(
76 21
                pathinfo($resource, PATHINFO_EXTENSION),
77 21
                ['xml', 'yml', 'yaml']
78
            );
79
    }
80
81
    /**
82
     * Returns controller locator by it's id.
83
     *
84
     * @param string $controller
85
     *
86
     * @throws \InvalidArgumentException
87
     *
88
     * @return array
89
     */
90 39
    private function getControllerLocator($controller)
91
    {
92 39
        $class = null;
93 39
        $prefix = null;
94
95 39
        if (0 === strpos($controller, '@')) {
96
            $file = $this->locator->locate($controller);
97
            $controllerClass = ClassUtils::findClassInFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file defined by $this->locator->locate($controller) on line 96 can also be of type array; however, FOS\RestBundle\Routing\L...tils::findClassInFile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
98
99
            if (false === $controllerClass) {
100
                throw new \InvalidArgumentException(sprintf('Can\'t find class for controller "%s"', $controller));
101
            }
102
103
            $controller = $controllerClass;
104
        }
105
106 39
        if ($this->container->has($controller)) {
107
            // service_id
108 12
            $prefix = $controller.'::';
109
110 12
            $class = get_class($this->container->get($controller));
111 28
        } elseif (class_exists($controller)) {
112
            // full class name
113 28
            $class = $controller;
114 28
            $prefix = $class.'::';
115
        }
116
117 39
        if (empty($class)) {
118
            throw new \InvalidArgumentException(sprintf('Class could not be determined for Controller identified by "%s".', $controller));
119
        }
120
121 39
        return [$prefix, $class];
122
    }
123
}
124