Completed
Pull Request — master (#36)
by Pavlo
02:17
created

Template   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 23
Bugs 3 Features 10
Metric Value
wmc 4
c 23
b 3
f 10
lcom 0
cbo 4
dl 0
loc 115
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __handler() 0 13 1
B __menu() 0 40 2
A __e404() 0 19 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: egorov
5
 * Date: 08.04.2015
6
 * Time: 13:22
7
 */
8
namespace samsoncms\template;
9
10
use samson\core\CompressableExternalModule;
11
use samsonphp\event\Event;
12
13
/**
14
 * Base SamsonCMS template controller
15
 * @package samsoncms\template
16
 */
17
class Template extends CompressableExternalModule
18
{
19
    /** Event when main page rendering has started */
20
    const E_MAIN_STARTED = 'template.main.started';
21
22
    /** Event when main page rendering has finished */
23
    const E_MAIN_RENDERED= 'template.main.rendered';
24
25
    /** Event when #template-menu rendering has started */
26
    const E_MENU_STARTED = 'template.menu.started';
27
28
    /** Event when #template-menu rendering has finished */
29
    const E_MENU_RENDERED = 'template.menu.rendered';
30
31
    /** Event when #e404 rendering has started */
32
    const E_E404_STARTED = 'template.e404.started';
33
34
    /** Event when #e404 rendering has finished */
35
    const E_E404_RENDERED = 'template.e404.rendered';
36
37
38
    /** @var bool Flag to show SamsonCMS logo in menu */
39
    public $menuLogo = 'www/menu/img/logo_w.png';
40
41
42
    /** @var string Module identifier */
43
    protected $id = 'template';
44
45
    /** Subscribed to e404 */
46
    //p0ublic function init(array $params = array())
47
    //{
48
    //    Event::subscribe('core.e404', array($this, '__e404'));
49
    //}
50
51
    /**
52
     * Universal controller action, this is SamsonCMS main page
53
     * rendering.
54
     */
55
    public function __handler()
56
    {
57
        // HTML main #template-container
58
        $html = '';
59
60
        Event::fire('template.main.started', array(&$html));
61
        Event::fire('template.main.rendered', array(&$html));
62
63
        // Prepare view
64
        $this->view('container')
65
            ->title(t('Главная', true))
66
            ->set('template-container', $html);
67
    }
68
69
    /** #template-menu rendering controller action */
70
    public function __menu()
71
    {
72
        // HTML main #template-menu
73
        $html = '';
74
        $submenu = '';
75
76
        $html .= $this->view('menu/item')
77
                ->set('icon', 'home')
78
                ->set('name', t('Главная', true))
79
                ->set('class', 'homeLink')
80
                ->set('active', url()->module == '' ? 'active' : '')
81
                ->output();
82
83
        $html .= $this->view('menu/item')
84
            ->set('icon', 'globe')
85
            ->set('id', '../')
86
            ->set('name', t('На сайт', true))
87
            ->set('target', '_blank')
88
            ->output();
89
90
        Event::fire('template.menu.started', array(&$html, &$submenu));
91
92
        $html .= $this->view('menu/item')
93
            ->set('icon', 'sign-out')
94
            ->set('class', 'sign-out')
95
            ->set('id', 'signin/logout')
96
            ->set('name', t('Выход', true))
97
            ->output();
98
99
        Event::fire('template.menu.rendered', array(&$html, &$submenu));
100
101
        // Prepare view
102
        $this->view('menu/index')
103
            // TODO: Remove samson\core\Core dependency
104
            ->set('module', url()->module)
105
            ->set('logo', $this->menuLogo)
106
107
            ->set('template-menu', $html)
108
            ->set('submenu', $submenu);
109
    }
110
111
    /** E404 controller action */
112
    function __e404()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for __e404.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
113
    {
114
        // HTML main #template-container
115
        $html = '';
116
117
        $this->system->active($this);
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\template\Template>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
118
119
        Event::fire('template.e404.started', array(&$html));
120
        Event::fire('template.e404.rendered', array(&$html));
121
122
        // Render template e404 into local module
123
        m('local')->html(
124
            $this->view('e404')
125
            ->set('template-container', $html)
126
            ->output()
127
        )->title(t('Страница не найдена', true));
128
129
        header("HTTP/1.0 404 Not Found");
130
    }
131
}
132