Ajde_Cms   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 93
Duplicated Lines 39.78 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
dl 37
loc 93
rs 10
c 0
b 0
f 0
wmc 15
lcom 2
cbo 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 6 2
A __construct() 0 3 1
A __bootstrap() 0 8 1
A setHomepage() 0 16 4
A detectNodeSlug() 19 19 3
A detectShopSlug() 18 18 3
A getRoutedNode() 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
class Ajde_Cms extends Ajde_Object_Singleton implements Ajde_BootstrapInterface
4
{
5
    private $_homepageSet = false;
6
7
    /**
8
     * @var NodeModel|bool
9
     */
10
    private $_detectedNode = false;
11
12
    public static function getInstance()
13
    {
14
        static $instance;
15
16
        return $instance === null ? $instance = new self() : $instance;
17
    }
18
19
    protected function __construct()
20
    {
21
    }
22
23
    public function __bootstrap()
24
    {
25
        Ajde_Event::register('Ajde_Core_Route', 'onAfterLangSet', [$this, 'setHomepage']);
26
        Ajde_Event::register('Ajde_Core_Route', 'onAfterRouteSet', [$this, 'detectNodeSlug']);
27
        Ajde_Event::register('Ajde_Core_Route', 'onAfterRouteSet', [$this, 'detectShopSlug']);
28
29
        return true;
30
    }
31
32
    public function setHomepage(Ajde_Core_Route $route)
0 ignored issues
show
Unused Code introduced by
The parameter $route is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        if ($this->_homepageSet) {
35
            return;
36
        }
37
        $this->_homepageSet = true;
38
39
        $homepageNodeId = (int) SettingModel::byName('homepage');
40
41
        if ($homepageNodeId) {
42
            $node = NodeModel::fromPk($homepageNodeId);
43
            if ($node) {
44
                Config::set('routes.homepage', $node->getUrl());
45
            }
46
        }
47
    }
48
49 View Code Duplication
    public function detectNodeSlug(Ajde_Core_Route $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...
50
    {
51
        $slug = $route->getRoute();
52
53
        $slug = trim($slug, '/');
54
        $lastSlash = strrpos($slug, '/');
55
        if ($lastSlash !== false) {
56
            $slug = substr($slug, $lastSlash + 1);
57
        }
58
59
        $node = NodeModel::fromSlug($slug);
60
        if ($node) {
61
            $this->_detectedNode = $node;
62
            $route->setRoute($slug);
63
            $routes = config('routes.list');
64
            array_unshift($routes, ['%^('.preg_quote($slug).')$%' => ['slug']]);
65
            Config::set('routes.list', $routes);
66
        }
67
    }
68
69 View Code Duplication
    public function detectShopSlug(Ajde_Core_Route $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...
70
    {
71
        $slug = $route->getRoute();
72
73
        $slug = trim($slug, '/');
74
        $lastSlash = strrpos($slug, '/');
75
        if ($lastSlash !== false) {
76
            $lastSlugPart = substr($slug, $lastSlash + 1);
77
78
            $product = ProductModel::fromSlug($lastSlugPart);
79
            if ($product) {
80
                $route->setRoute($slug);
81
                $routes = config('routes.list');
82
                array_unshift($routes, ['%^(shop)/('.preg_quote($lastSlugPart).')$%' => ['module', 'slug']]);
83
                Config::set('routes.list', $routes);
84
            }
85
        }
86
    }
87
88
    /**
89
     * @return NodeModel|bool
90
     */
91
    public function getRoutedNode()
92
    {
93
        return $this->_detectedNode;
94
    }
95
}
96