Completed
Push — master ( 89b44b...f25d12 )
by Vitaly
06:37
created

Application   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 16
Bugs 5 Features 5
Metric Value
wmc 36
c 16
b 5
f 5
lcom 1
cbo 4
dl 0
loc 199
rs 8.8

12 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 31 5
A isModuleDependent() 0 4 2
A isCMS() 0 4 1
A activeModuleHandler() 0 13 2
A updateCMSPrefix() 0 6 3
A buildUrl() 0 11 3
A parseUrl() 0 11 3
A ifModuleRelated() 0 5 1
A __base() 0 10 1
A oldMainRenderer() 0 10 3
D oldMenuRenderer() 0 35 9
A oldGetTitle() 0 8 3
1
<?php
2
namespace samsoncms\cms;
3
4
use samson\core\CompressableExternalModule;
5
use samson\core\SamsonLocale;
6
use samsonphp\event\Event;
7
use samsonphp\router\Module;
8
9
/**
10
 * SamsonCMS external compressible application for integrating
11
 * @author Vitaly Iegorov <[email protected]>
12
 */
13
class Application extends CompressableExternalModule
0 ignored issues
show
Deprecated Code introduced by
The class samson\core\CompressableExternalModule has been deprecated with message: Just implement samsonframework\core\CompressInterface

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
14
{
15
    const EVENT_IS_CMS = 'samsonsms.is.cms';
16
17
    /** @var string Module identifier */
18
    public $id = 'cms';
19
20
    public $baseUrl = 'cms';
21
22
    /** @var bool Flag that currently we are woring in SamsonCMS */
23
    protected $isCMS = false;
24
25
    public function init(array $params = array())
26
    {
27
        //trace('cmsInit');
28
        // Old applications main page rendering
29
        Event::subscribe('template.main.rendered', array($this, 'oldMainRenderer'));
30
        // Old applications menu rendering
31
        Event::subscribe('template.menu.rendered', array($this, 'oldMenuRenderer'));
32
33
        Event::subscribe('samson.url.build', array($this, 'buildUrl'));
34
35
        Event::subscribe('samson.url.args.created', array($this, 'parseUrl'));
36
37
        Event::subscribe(Module::EVENT_ROUTE_FOUND, array($this, 'activeModuleHandler'));
38
39
        Event::subscribe('samsonphp.router.create.module.routes', array($this, 'updateCMSPrefix'));
40
41
        //[PHPCOMPRESSOR(remove,start)]
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
        $moduleList   = $this->system->module_stack;
0 ignored issues
show
Bug introduced by
Accessing module_stack on the interface samsonframework\core\SystemInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
43
        foreach ($this->system->module_stack as $id => $module) {
0 ignored issues
show
Bug introduced by
Accessing module_stack on the interface samsonframework\core\SystemInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
44
            if (!$this->isModuleDependent($module) && $id != 'core' && !$this->ifModuleRelated($module)) {
45
                unset($moduleList[$id]);
46
            }
47
        }
48
49
        // Generate resources for new module
50
        $this->system->module('resourcer')->generateResources($moduleList, $this->path() . 'app/view/index.php');
51
        //[PHPCOMPRESSOR(remove,end)]
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
53
        // Call parent initialization
54
        return parent::init($params);
55
    }
56
57
    /**
58
     * If module is dependent from current module through composer.json.
59
     *
60
     * @param $module Module for checking
61
     * @return bool True if module dependent
62
     */
63
    protected function isModuleDependent($module)
64
    {
65
        return isset($module->composerParameters['composerName']) && in_array($module->composerParameters['composerName'], $this->composerParameters['required']);
66
    }
67
68
    public function isCMS()
69
    {
70
        return $this->isCMS;
71
    }
72
73
    public function activeModuleHandler($module)
74
    {
75
        // Define if routed module is related to SamsonCMS
76
        if($this->isCMS = $this->ifModuleRelated($module)){
77
            // TODO: This should be removed - Reparse url
78
            url()->parse();
79
80
            // Switch template to SamsonCMS
81
            $this->system->template($this->path() . 'app/view/index.php', true);
82
83
            Event::fire(self::EVENT_IS_CMS, array(&$this));
84
        }
85
    }
86
87
    /**
88
     * Callback for adding SamsonCMS related modules prefix to routes.
89
     *
90
     * @param $module
91
     * @param $prefix
92
     */
93
    public function updateCMSPrefix($module, &$prefix)
94
    {
95
        if (($module->id != $this->id) && $this->ifModuleRelated($module)) {
96
            $prefix = '/' . $this->baseUrl . $prefix;
97
        }
98
    }
99
100
    public function buildUrl(&$urlObj, &$httpHost, &$urlParams)
101
    {
102
        if ($this->isCMS) {
103
            if (in_array($urlParams[0], SamsonLocale::get(), true)) {
104
                array_splice($urlParams, 1, 0, array($this->baseUrl));
105
                $urlParams = array_values($urlParams);
106
            } else {
107
                array_unshift($urlParams, $this->baseUrl);
108
            }
109
        }
110
    }
111
112
    public function parseUrl(&$urlObj, &$urlArgs)
113
    {
114
        if ($this->isCMS) {
115
            if (in_array($urlArgs[0], SamsonLocale::get(), true)) {
116
                unset($urlArgs[1]);
117
                $urlArgs = array_values($urlArgs);
118
            } else {
119
                array_shift($urlArgs);
120
            }
121
        }
122
    }
123
124
    /**
125
     * Check if passed module is related to SamsonCMS.
126
     * Also method stores data to flag variable.
127
     *
128
     * @param $module
129
     *
130
     * @return bool True if module related to SamsonCMS
131
     */
132
    public function ifModuleRelated($module)
133
    {
134
        // Analyze if module class or one of its parents has samsoncms\ namespace pattern
135
        return sizeof(preg_grep('/samsoncms\\\\/i', array_merge(array(get_class($module)), class_parents($module))));
136
    }
137
138
    public function __base()
139
    {
140
        $templateModule = $this->system->module('template');
141
142
        // Switch system to SamsonCMS template module
143
        $this->system->active($templateModule);
144
145
        // Call template handler
146
        $templateModule->__handler();
147
    }
148
149
    public function oldMainRenderer(&$html)
150
    {
151
        // Render application main page block
152
        foreach (\samsoncms\Application::loaded() as $app) {
0 ignored issues
show
Deprecated Code introduced by
The method samsoncms\Application::loaded() has been deprecated.

This method has been deprecated.

Loading history...
153
            // Show only visible apps
154
            if ($app->hide == false /*&& $app->findView('sub_menu')*/) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
155
                $html .= $app->main();
0 ignored issues
show
Deprecated Code introduced by
The method samsoncms\Application::main() has been deprecated with message: Subscribe to samsoncms/template event

This method 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 method will be removed from the class and what other method or class to use instead.

Loading history...
156
            }
157
        }
158
    }
159
160
    /**
161
     * @deprecated All application should draw menu block via events
162
     */
163
    public function oldMenuRenderer(&$html, &$subMenu)
0 ignored issues
show
Coding Style introduced by
oldMenuRenderer uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
164
    {
165
        // Iterate loaded samson\cms\application
166
        foreach (\samsoncms\Application::loaded() as $app) {
0 ignored issues
show
Deprecated Code introduced by
The method samsoncms\Application::loaded() has been deprecated.

This method has been deprecated.

Loading history...
167
            // Show only visible apps
168
            if ($app->hide == false) {
169
                // Render application menu item
170
                $html .= m('template')
0 ignored issues
show
Deprecated Code introduced by
The function m() has been deprecated with message: Use $this->system->module() in module context

This function has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
171
                    ->view('menu/item')
172
                    ->active(url()->module == $app->id() ? 'active' : '')
173
                    ->app($app)
174
                    ->icon($app->icon)
175
                    ->name(isset($app->name{0}) ? $app->name : '')
176
                    ->output();
177
            }
178
        }
179
        $subMenu = '';
180
        // Find current SamsonCMS application
181
        if (\samsoncms\Application::find(url()->module, $app/*@var $app App*/)) {
182
            // If module has sub_menu view - render it
183
            if ($app->findView('sub_menu')) {
184
                // Explode url by symbols '/'
185
                $url = explode('/', $_SERVER['REQUEST_URI']);
186
                // If isset url with params search and param page equal 0
187
                if (isset($url[4]) && $url[3] != 'form') {
188
                    // Default value for search field
189
                    $paramSearch = urldecode($url[4]);
190
                    // Set params search
191
                    $app->set($paramSearch, 'search');
192
                }
193
				
194
                $subMenu .= $app->view('sub_menu')->set(t($app->name, true), 'appName')->output();
195
            }
196
        }
197
    }
198
199
    /**
200
     * @deprecated
201
     * @return string Page title
202
     */
203
    public function oldGetTitle()
204
    {
205
        $local   = m('local');
0 ignored issues
show
Deprecated Code introduced by
The function m() has been deprecated with message: Use $this->system->module() in module context

This function has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
206
        $current = m();
0 ignored issues
show
Deprecated Code introduced by
The function m() has been deprecated with message: Use $this->system->module() in module context

This function has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
207
208
        return isset($current['title']) ? $current['title'] :
209
            (isset($local['title']) ? $local['title'] : '');
210
    }
211
}
212