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

RouteService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 21.74 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 20
loc 92
ccs 28
cts 35
cp 0.8
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createRoute() 10 10 1
A __construct() 0 4 1
A dispatchRouteEvent() 0 4 1
A fillRoute() 0 22 3
A updateRoute() 10 10 1
A generatePath() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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