MetricRouteLoader::load()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 15
ccs 0
cts 8
cp 0
rs 10
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Lamoda\Metric\MetricBundle\Routing;
4
5
use Symfony\Component\Config\Loader\LoaderInterface;
6
use Symfony\Component\Config\Loader\LoaderResolverInterface;
7
use Symfony\Component\Routing\Route;
8
use Symfony\Component\Routing\RouteCollection;
9
10
final class MetricRouteLoader implements LoaderInterface
11
{
12
    private $controllers = [];
13
14
    private $loaded = false;
15
16
    /**
17
     * Add controllers on path for registration.
18
     *
19
     * @param string $name
20
     * @param string $path
21
     * @param string $serviceId
22
     * @param string $method
23
     *
24
     * @throws \LogicException
25
     */
26
    public function registerController(string $name, string $path, string $serviceId, string $method = '__invoke')
27
    {
28
        if (array_key_exists($path, $this->controllers)) {
29
            throw new \LogicException('Cannot register metric controller on the same path twice');
30
        }
31
32
        $this->controllers[$name] = [$path, $serviceId, $method];
33
    }
34
35
    /** {@inheritdoc} */
36
    public function load($resource, $type = null): RouteCollection
37
    {
38
        if ($this->loaded) {
39
            throw new \LogicException('Lamoda metrics routes have been already loaded');
40
        }
41
42
        $collection = new RouteCollection();
43
44
        foreach ($this->controllers as $name => list($path, $controller)) {
45
            $collection->add($controller, new Route($path, ['_controller' => $controller]));
46
        }
47
48
        $this->loaded = true;
49
50
        return $collection;
51
    }
52
53
    /** {@inheritdoc} */
54
    public function supports($resource, $type = null): bool
55
    {
56
        return 'lamoda_metrics' === $type;
57
    }
58
59
    /** {@inheritdoc} */
60
    public function getResolver()
61
    {
62
    }
63
64
    /** {@inheritdoc} */
65
    public function setResolver(LoaderResolverInterface $resolver)
66
    {
67
    }
68
}
69