Completed
Push — master ( bec163...ff0d9e )
by Leny
11s
created

RouteLoader   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 123
Duplicated Lines 24.39 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 4
dl 30
loc 123
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addVictoireRouting() 0 19 2
A addWidgetsRouting() 0 10 3
A addShowBusinessPageByIdAction() 0 13 1
A load() 0 13 1
A addShowPageByIdRoute() 0 17 1
A addShowPageRoute() 15 15 1
A addShowHomePageRoute() 15 15 1
A supports() 0 4 1

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
namespace Victoire\Bundle\CoreBundle\Route;
4
5
use Symfony\Component\Config\Loader\Loader;
6
use Symfony\Component\Routing\Route;
7
use Symfony\Component\Routing\RouteCollection;
8
9
class RouteLoader extends Loader
10
{
11
    protected $widgets;
12
13
    public function __construct($widgets)
14
    {
15
        $this->widgets = $widgets;
16
    }
17
18
    public function load($resource, $type = null)
19
    {
20
        $collection = new RouteCollection();
21
22
        $this->addVictoireRouting($collection);
23
        $this->addWidgetsRouting($collection);
24
        $this->addShowPageByIdRoute($collection);
25
        $this->addShowBusinessPageByIdAction($collection);
26
        $this->addShowPageRoute($collection);
27
        $this->addShowHomePageRoute($collection);
28
29
        return $collection;
30
    }
31
32
    protected function addVictoireRouting(RouteCollection &$collection)
33
    {
34
        $resources = [
35
            '@VictoireAnalyticsBundle/Controller/',
36
            '@VictoireTemplateBundle/Controller/',
37
            '@VictoireBlogBundle/Controller/',
38
            '@VictoireBusinessPageBundle/Controller/',
39
            '@VictoireSeoBundle/Controller/',
40
            '@VictoireMediaBundle/Controller/',
41
            '@VictoirePageBundle/Controller/',
42
            '@VictoireCoreBundle/Controller/',
43
            '@VictoireWidgetBundle/Controller/',
44
            '@VictoireSitemapBundle/Controller/',
45
        ];
46
        foreach ($resources as $resource) {
47
            $importedRoutes = $this->import($resource, 'annotation');
48
            $collection->addCollection($importedRoutes);
49
        }
50
    }
51
52
    protected function addWidgetsRouting(RouteCollection &$collection)
53
    {
54
        foreach ($this->widgets as $widgetParams) {
55
            $controllerResource = '@VictoireWidget'.$widgetParams['name'].'Bundle/Controller/';
56
            if ($this->getResolver()->resolve($controllerResource)) {
57
                $importedRoutes = $this->import($controllerResource, 'annotation');
58
                $collection->addCollection($importedRoutes);
59
            }
60
        }
61
    }
62
63
    protected function addShowBusinessPageByIdAction(RouteCollection &$collection)
64
    {
65
        $pattern = '/victoire-dcms-public/show-business-page-by-id/{entityId}/{type}';
66
        $defaults = [
67
            '_controller' => 'VictoirePageBundle:Page:showBusinessPageById',
68
        ];
69
        $requirements = [
70
            'viewId' => '\d+',
71
        ];
72
        $route = new Route($pattern, $defaults, $requirements);
73
        $routeName = 'victoire_core_business_page_show_by_id';
74
        $collection->add($routeName, $route);
75
    }
76
77
    protected function addShowPageByIdRoute(RouteCollection &$collection)
78
    {
79
        $pattern = '/victoire-dcms-public/show-page-by-id/{viewId}/{entityId}';
80
        $defaults = [
81
            '_controller' => 'VictoirePageBundle:Page:showById',
82
            'entityId'    => null,
83
        ];
84
        $requirements = [
85
            'viewId' => '\d+',
86
        ];
87
        $options = [
88
            'expose' => true,
89
        ];
90
        $route = new Route($pattern, $defaults, $requirements, $options);
91
        $routeName = 'victoire_core_page_show_by_id';
92
        $collection->add($routeName, $route);
93
    }
94
95 View Code Duplication
    protected function addShowPageRoute(RouteCollection &$collection)
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...
96
    {
97
        // prepare a new route
98
        $pattern = '/{url}';
99
        $defaults = [
100
            '_controller' => 'VictoirePageBundle:Page:show',
101
        ];
102
        $requirements = [
103
            'url' => '^.*$',
104
        ];
105
        $route = new Route($pattern, $defaults, $requirements);
106
107
        // add the new route to the route collection:
108
        $collection->add('victoire_core_page_show', $route);
109
    }
110
111 View Code Duplication
    protected function addShowHomePageRoute(RouteCollection &$collection)
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...
112
    {
113
        // prepare a new route
114
        $pattern = '/';
115
        $defaults = [
116
            '_controller' => 'VictoirePageBundle:Page:show',
117
        ];
118
        $options = [
119
            'expose' => true,
120
        ];
121
        $route = new Route($pattern, $defaults, [], $options);
122
123
        // add the new route to the route collection:
124
        $collection->add('victoire_core_homepage_show', $route);
125
    }
126
127
    public function supports($resource, $type = null)
128
    {
129
        return $type === 'victoire';
130
    }
131
}
132