Ajde_Cms::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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