Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

RouteService::fillRoute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 14
cts 15
cp 0.9333
rs 9.2
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 1
crap 3.0026
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\Service;
16
17
use SWP\Bundle\ContentBundle\Event\RouteEvent;
18
use SWP\Bundle\ContentBundle\Model\RouteInterface;
19
use SWP\Bundle\ContentBundle\RouteEvents;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22
class RouteService implements RouteServiceInterface
23
{
24
    /**
25
     * @var EventDispatcherInterface
26
     */
27
    private $eventDispatcher;
28
29
    /**
30
     * RouteService constructor.
31
     *
32
     * @param EventDispatcherInterface $eventDispatcher
33
     */
34 48
    public function __construct(EventDispatcherInterface $eventDispatcher)
35
    {
36 48
        $this->eventDispatcher = $eventDispatcher;
37 48
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 29 View Code Duplication
    public function createRoute(RouteInterface $route)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44 29
        $this->dispatchRouteEvent(RouteEvents::PRE_CREATE, $route);
45
46 29
        $route = $this->fillRoute($route);
47
48 29
        $this->dispatchRouteEvent(RouteEvents::POST_CREATE, $route);
49
50 29
        return $route;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 5 View Code Duplication
    public function updateRoute(RouteInterface $route)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58 5
        $this->dispatchRouteEvent(RouteEvents::PRE_UPDATE, $route);
59
60 5
        $route = $this->fillRoute($route);
61
62 5
        $this->dispatchRouteEvent(RouteEvents::POST_UPDATE, $route);
63
64 5
        return $route;
65
    }
66
67 30
    private function dispatchRouteEvent($eventName, RouteInterface $route)
68
    {
69 30
        $this->eventDispatcher->dispatch($eventName, new RouteEvent($route));
70 30
    }
71
72
    /**
73
     * @param RouteInterface $route
74
     *
75
     * @return RouteInterface
76
     */
77 48
    public function fillRoute(RouteInterface $route)
78
    {
79 48
        switch ($route->getType()) {
80 48
            case RouteInterface::TYPE_CONTENT:
81 28
                $route->setVariablePattern(null);
82 28
                $route->setStaticPrefix($this->generatePath($route));
83 28
                $route->setRequirements([]);
84
85 28
                break;
86 41
            case RouteInterface::TYPE_COLLECTION:
87 41
                $route->setVariablePattern('/{slug}');
88 41
                $route->setStaticPrefix($this->generatePath($route));
89 41
                $route->setRequirement('slug', '[a-zA-Z0-9*\-_\/]+');
90 41
                $route->setDefault('slug', null);
91
92 41
                break;
93
            default:
94
                throw new \InvalidArgumentException(sprintf('Route type "%s" is unsupported!', $route->getType()));
95
        }
96
97 48
        return $route;
98
    }
99
100
    /**
101
     * @param RouteInterface $route
102
     *
103
     * @return string
104
     */
105 48
    protected function generatePath(RouteInterface $route)
106
    {
107 48
        if (null === $parent = $route->getParent()) {
108 48
            return '/'.$route->getName();
109
        }
110
111 2
        return sprintf('%s/%s', $parent->getStaticPrefix(), $route->getName());
112
    }
113
}
114