Completed
Pull Request — master (#29)
by Dima
02:24
created

Application::oldMenuRenderer()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 39
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 3
Metric Value
c 5
b 1
f 3
dl 0
loc 39
rs 4.909
cc 9
eloc 20
nc 12
nop 2
1
<?php
2
namespace samsoncms\cms;
3
4
use samson\core\CompressableExternalModule;
5
use samsonphp\event\Event;
6
use samsonphp\router\Module;
7
8
/**
9
 * SamsonCMS external compressible application for integrating
10
 * @author Vitaly Iegorov <[email protected]>
11
 */
12
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...
13
{
14
    const EVENT_IS_CMS = 'samsonsms.is.cms';
15
16
    /** @var string Module identifier */
17
    public $id = 'cms';
18
19
    public $baseUrl = 'cms';
20
21
    /** @var bool Flag that currently we are woring in SamsonCMS */
22
    protected $isCMS = false;
23
24
    public function init(array $params = array())
25
    {
26
        //trace('cmsInit');
27
        // Old applications main page rendering
28
        Event::subscribe('template.main.rendered', array($this, 'oldMainRenderer'));
29
        // Old applications menu rendering
30
        Event::subscribe('template.menu.rendered', array($this, 'oldMenuRenderer'));
31
32
        Event::subscribe('samson.url.build', array($this, 'buildUrl'));
33
34
        Event::subscribe('samson.url.args.created', array($this, 'parseUrl'));
35
36
        Event::subscribe(Module::EVENT_ROUTE_FOUND, array($this, 'activeModuleHandler'));
37
38
        Event::subscribe('samsonphp.router.create.module.routes', array($this, 'updateCMSPrefix'));
39
40
        //[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...
41
        $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...
42
        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...
43
            if (!$this->isModuleDependent($module) && $id != 'core' && !$this->ifModuleRelated($module)) {
44
                unset($moduleList[$id]);
45
            }
46
        }
47
48
        // Generate resources for new module
49
        $this->system->module('resourcer')->generateResources($moduleList, $this->path() . 'app/view/index.php');
50
        //[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...
51
52
        // Call parent initialization
53
        return parent::init($params);
54
    }
55
56
    /**
57
     * If module is dependent from current module through composer.json.
58
     *
59
     * @param $module Module for checking
60
     * @return bool True if module dependent
61
     */
62
    protected function isModuleDependent($module)
63
    {
64
        return isset($module->composerParameters['composerName']) && in_array($module->composerParameters['composerName'], $this->composerParameters['required']);
65
    }
66
67
    public function isCMS()
68
    {
69
        return $this->isCMS;
70
    }
71
72
    public function activeModuleHandler($module)
73
    {
74
        // Define if routed module is related to SamsonCMS
75
        if($this->isCMS = $this->ifModuleRelated($module)){
76
            // TODO: This should be removed - Reparse url
77
            url()->parse();
78
79
            // Switch template to SamsonCMS
80
            $this->system->template($this->path() . 'app/view/index.php', true);
81
82
            Event::fire(self::EVENT_IS_CMS, array(&$this));
83
        }
84
    }
85
86
    /**
87
     * Callback for adding SamsonCMS related modules prefix to routes.
88
     *
89
     * @param $module
90
     * @param $prefix
91
     */
92
    public function updateCMSPrefix($module, &$prefix)
93
    {
94
        if (($module->id != $this->id) && $this->ifModuleRelated($module)) {
95
            $prefix = '/' . $this->baseUrl . $prefix;
96
        }
97
    }
98
99
    public function buildUrl(&$urlObj, &$httpHost, &$urlParams)
100
    {
101
        if ($this->isCMS) {
102
            array_unshift($urlParams, $this->baseUrl);
103
        }
104
    }
105
106
    public function parseUrl(&$urlObj, &$urlArgs)
107
    {
108
        if ($this->isCMS) {
109
            array_shift($urlArgs);
110
        }
111
    }
112
113
    /**
114
     * Check if passed module is related to SamsonCMS.
115
     * Also method stores data to flag variable.
116
     *
117
     * @param $module
118
     *
119
     * @return bool True if module related to SamsonCMS
120
     */
121
    public function ifModuleRelated($module)
122
    {
123
        // Analyze if module class or one of its parents has samsoncms\ namespace pattern
124
        return sizeof(preg_grep('/samsoncms\\\\/i', array_merge(array(get_class($module)), class_parents($module))));
125
    }
126
127
    public function __base()
128
    {
129
        $templateModule = $this->system->module('template');
130
131
        // Switch system to SamsonCMS template module
132
        $this->system->active($templateModule);
133
134
        // Call template handler
135
        $templateModule->__handler();
136
    }
137
138
    public function oldMainRenderer(&$html)
139
    {
140
        // Render application main page block
141
        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...
142
            // Show only visible apps
143
            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...
144
                $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...
145
            }
146
        }
147
    }
148
149
    /**
150
     * @deprecated All application should draw menu block via events
151
     */
152
    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...
153
    {
154
        // Iterate loaded samson\cms\application
155
        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...
156
            // Show only visible apps
157
            if ($app->hide == false) {
158
                // Render application menu item
159
                $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...
160
                    ->view('menu/item')
161
                    ->active(url()->module == $app->id() ? 'active' : '')
162
                    ->app($app)
163
                    ->icon($app->icon)
164
                    ->name(isset($app->name{0}) ? $app->name : '')
165
                    ->output();
166
            }
167
        }
168
        $subMenu = '';
169
        // Find current SamsonCMS application
170
        if (\samsoncms\Application::find(url()->module, $app/*@var $app App*/)) {
171
            // If module has sub_menu view - render it
172
            if ($app->findView('sub_menu')) {
173
		// Get url params
174
                $url =  $_SERVER['REQUEST_URI'];
175
                // Explode url by symbols '/'
176
                $url = explode('/', $url);
177
                // If isset url with params search and param page equal 0
178
                if (isset($url[4]) && $url[3] == '0') {
179
                    // Default value for search field
180
                    $paramSearch = '';
0 ignored issues
show
Unused Code introduced by
$paramSearch is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
181
                    // Default value for search field
182
                    $paramSearch = urldecode($url[4]);
183
                    // Set params search
184
                    $app->search($paramSearch);
185
                }
186
				
187
                $subMenu .= $app->view('sub_menu')->set(t($app->name, true), 'appName')->output();
188
            }
189
        }
190
    }
191
192
    /**
193
     * @deprecated
194
     * @return string Page title
195
     */
196
    public function oldGetTitle()
197
    {
198
        $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...
199
        $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...
200
201
        return isset($current['title']) ? $current['title'] :
202
            (isset($local['title']) ? $local['title'] : '');
203
    }
204
}
205