Completed
Push — master ( f93802...a3beff )
by Paweł
34:14
created

RouteService::generatePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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 21
    public function __construct(EventDispatcherInterface $eventDispatcher)
35
    {
36 21
        $this->eventDispatcher = $eventDispatcher;
37 21
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 14 View Code Duplication
    public function createRoute(RouteInterface $route)
43
    {
44 14
        $this->dispatchRouteEvent(RouteEvents::PRE_CREATE, $route);
45
46 14
        $route = $this->fillRoute($route);
47
48 14
        $this->dispatchRouteEvent(RouteEvents::POST_CREATE, $route);
49
50 14
        return $route;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 View Code Duplication
    public function updateRoute(RouteInterface $route)
57
    {
58
        $this->dispatchRouteEvent(RouteEvents::PRE_UPDATE, $route);
59
60
        $route = $this->fillRoute($route);
61
62
        $this->dispatchRouteEvent(RouteEvents::POST_UPDATE, $route);
63
64
        return $route;
65
    }
66
67 14
    private function dispatchRouteEvent($eventName, RouteInterface $route)
68
    {
69 14
        $this->eventDispatcher->dispatch($eventName, new RouteEvent($route));
70 14
    }
71
72
    /**
73
     * @param RouteInterface $route
74
     *
75
     * @return RouteInterface
76
     */
77 21
    public function fillRoute(RouteInterface $route)
78
    {
79 21
        switch ($route->getType()) {
80 21
            case RouteInterface::TYPE_CONTENT:
81 10
                $route->setVariablePattern(null);
82 10
                $route->setStaticPrefix($this->generatePath($route));
83 10
                $route->setRequirements([]);
84
85 10
                break;
86 18
            case RouteInterface::TYPE_COLLECTION:
87 18
                $route->setVariablePattern('/{slug}');
88 18
                $route->setStaticPrefix($this->generatePath($route));
89 18
                $route->setRequirement('slug', '[a-zA-Z0-9*\-_\/]+');
90 18
                $route->setDefault('slug', null);
91
92 18
                break;
93
            default:
94
                throw new \InvalidArgumentException(sprintf('Route type "%s" is unsupported!', $route->getType()));
95
        }
96
97 21
        return $route;
98
    }
99
100
    /**
101
     * @param RouteInterface $route
102
     *
103
     * @return string
104
     */
105 21
    protected function generatePath(RouteInterface $route)
106
    {
107 21
        if (null === $parent = $route->getParent()) {
108 21
            return '/'.$route->getName();
109
        }
110
111
        return sprintf('%s/%s', $parent->getStaticPrefix(), $route->getName());
112
    }
113
}
114