TagCategoryRouter::getRouteCollection()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 0
cts 48
cp 0
rs 8.2286
c 0
b 0
f 0
cc 6
nc 4
nop 0
crap 42

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kunstmaan\ArticleBundle\Router;
4
5
use Kunstmaan\NodeBundle\Router\SlugRouter;
6
use Symfony\Component\Routing\RouteCollection;
7
use Symfony\Component\Translation\TranslatorInterface;
8
9
class TagCategoryRouter extends SlugRouter
10
{
11
    /**
12
     * @return \Symfony\Component\Routing\RouteCollection
13
     */
14
    public function getRouteCollection()
15
    {
16
        if (!\is_null($this->routeCollection)) {
17
            return $this->routeCollection;
18
        }
19
        $this->routeCollection = new RouteCollection();
20
21
        $extendParameters = array('category' => null, 'tag' => null);
22
        $baseSlugParameters = array_merge($this->getSlugRouteParameters(), $extendParameters);
23
        $baseSlugPreviewParameters = array_merge($this->getPreviewRouteParameters(), $extendParameters);
24
25
        /** @var TranslatorInterface $translator */
26
        $translator = $this->container->get('translator');
0 ignored issues
show
Deprecated Code introduced by
The property Kunstmaan\NodeBundle\Router\SlugRouter::$container has been deprecated with message: in KunstmaanNodeBundle 5.1 and will be removed in KunstmaanNodeBundle 6.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
27
28
        if ($this->isMultiLanguage()) {
29
            foreach ($this->getFrontendLocales() as $locale) {
30
                $categoryTrans = $translator->trans('article_overview_page.route.category', [], null, $locale);
31
                $tagTrans = $translator->trans('article_overview_page.route.tag', [], null, $locale);
32
33
                $routePathParts = array(
34
                    '_slug_category_tag' => sprintf('/%s/{category}/%s/{tag}', $categoryTrans, $tagTrans),
35
                    '_slug_tag' => sprintf('/%s/{tag}', $tagTrans),
36
                    '_slug_category' => sprintf('/%s/{category}', $categoryTrans),
37
                );
38
39
                foreach ($routePathParts as $routeName => $routePart) {
40
                    $slugParameters = $baseSlugParameters;
41
                    $slugParameters['path'] = '/{_locale}/{url}' . $routePart;
42
43
                    $slugPreviewParameters = $baseSlugPreviewParameters;
44
                    $slugPreviewParameters['path'] = '/{_locale}/admin/preview/{url}' . $routePart;
45
46
                    $routeName .= '_' . $locale;
47
                    $this->addRoute($routeName . '_preview', $slugPreviewParameters);
48
                    $this->addRoute($routeName, $slugParameters);
49
                }
50
            }
51
        } else {
52
            $categoryTrans = $translator->trans('article_overview_page.route.category');
53
            $tagTrans = $translator->trans('article_overview_page.route.tag');
54
55
            $slugParameters = $baseSlugParameters;
56
            $slugPreviewParameters = $baseSlugPreviewParameters;
57
58
            $routePathParts = array(
59
                '_slug_category_tag' => sprintf('/%s/{category}/%s/{tag}', $categoryTrans, $tagTrans),
60
                '_slug_tag' => sprintf('/%s/{tag}', $tagTrans),
61
                '_slug_category' => sprintf('/%s/{category}', $categoryTrans),
62
            );
63
64
            foreach ($routePathParts as $routeName => $routePart) {
65
                $slugParameters['path'] = '/{url}' . $routePart;
66
                $slugPreviewParameters['path'] = '/admin/preview/{url}' . $routePart;
67
68
                $this->addRoute($routeName . '_preview', $slugPreviewParameters);
69
                $this->addRoute($routeName, $slugParameters);
70
            }
71
        }
72
73
        return $this->routeCollection;
74
    }
75
}
76