RoutingListener::addPrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Innmind\RestBundle\EventListener;
4
5
use Innmind\Rest\Server\Events;
6
use Innmind\Rest\Server\Event\RouteEvent;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
9
class RoutingListener implements EventSubscriberInterface
10
{
11
    protected $prefix;
12
13 4
    public function __construct($prefix)
14
    {
15 4
        $this->prefix = sprintf(
16 4
            '/%s',
17 4
            ltrim(rtrim($prefix, '/'), '/')
18 4
        );
19 4
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 2
    public static function getSubscribedEvents()
25
    {
26
        return [
27 2
            Events::ROUTE => 'addPrefix',
28 2
        ];
29
    }
30
31
    /**
32
     * Add a prefix on each route
33
     *
34
     * @param RouteEvent $event
35
     *
36
     * @return void
37
     */
38 2
    public function addPrefix(RouteEvent $event)
39
    {
40 2
        $route = $event->getRoute();
41
42 2
        $route->setPath($this->prefix . $route->getPath());
43 2
    }
44
}
45