AppController::beforeRender()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.5222
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Core
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Core".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Controller;
17
18
use Core\Core\Plugin;
19
use Cake\Event\Event;
20
use Cake\Http\Response;
21
use Core\Event\EventManager;
22
use Core\Controller\Component\AppComponent;
23
use Core\Controller\Component\MoveComponent;
24
use Core\Controller\Component\FlashComponent;
25
use Core\Controller\Component\ProcessComponent;
26
use Cake\Controller\Controller as CakeController;
27
use Cake\Controller\Component\RequestHandlerComponent;
28
29
/**
30
 * Class AppController
31
 *
32
 * @package     Core\Controller
33
 * @property    AppComponent $App
34
 * @property    MoveComponent $Move
35
 * @property    FlashComponent $Flash
36
 * @property    ProcessComponent $Process
37
 * @property    \Cake\Http\Response $response
38
 * @property    RequestHandlerComponent $RequestHandler
39
 */
40
class AppController extends CakeController
41
{
42
43
    /**
44
     * Initialization hook method.
45
     *
46
     * @return  void
47
     */
48
    public function initialize()
49
    {
50
        parent::initialize();
51
        $this->_setTheme();
52
53
        $pluginEvent = Plugin::getData('Core', 'Controller.initialize');
54
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('Controller.initialize')) {
55
            call_user_func_array($pluginEvent->find(0), [$this]);
56
        }
57
    }
58
59
    /**
60
     * Called after the controller action is run, but before the view is rendered. You can use this method
61
     * to perform logic or set view variables that are required on every request.
62
     *
63
     * @param   \Cake\Event\Event $event The beforeRender event.
64
     *
65
     * @return  void
66
     */
67
    public function beforeRender(Event $event)
68
    {
69
        $pluginEvent = Plugin::getData('Core', 'Controller.beforeRender');
70
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('Controller.beforeRender')) {
71
            call_user_func_array($pluginEvent->find(0), [$this, $event]);
72
        }
73
74
        if (!array_key_exists('_serialize', $this->viewVars) &&
75
            in_array($this->response->getType(), ['application/json', 'application/xml'])
76
        ) {
77
            $this->set('_serialize', true);
78
        }
79
    }
80
81
    /**
82
     * Called before the controller action. You can use this method to configure and customize components
83
     * or perform logic that needs to happen before each controller action.
84
     *
85
     * @param   Event $event
86
     *
87
     * @return  void
88
     */
89
    public function beforeFilter(Event $event)
90
    {
91
        EventManager::trigger('Controller.setup', $this);
92
93
        $pluginEvent = Plugin::getData('Core', 'Controller.beforeFilter');
94
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('Controller.beforeFilter')) {
95
            call_user_func_array($pluginEvent->find(0), [$this, $event]);
96
        }
97
    }
98
99
    /**
100
     * The beforeRedirect method is invoked when the controller's redirect method is called but before any
101
     * further action.
102
     *
103
     * @param   Event $event
104
     * @param   array|string $url
105
     * @param   Response $response
106
     *
107
     * @return  void
108
     */
109 View Code Duplication
    public function beforeRedirect(Event $event, $url, Response $response)
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...
110
    {
111
        $pluginEvent = Plugin::getData('Core', 'Controller.beforeRedirect');
112
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('Controller.beforeRedirect')) {
113
            call_user_func_array($pluginEvent->find(0), [$this, $event, $url, $response]);
114
        }
115
    }
116
117
    /**
118
     * Called after the controller action is run and rendered.
119
     *
120
     * @param   Event $event
121
     *
122
     * @return  void
123
     */
124
    public function afterFilter(Event $event)
125
    {
126
        $pluginEvent = Plugin::getData('Core', 'Controller.afterFilter');
127
        if (is_callable($pluginEvent->find(0)) && Plugin::hasManifestEvent('Controller.afterFilter')) {
128
            call_user_func_array($pluginEvent->find(0), [$this, $event]);
129
        }
130
    }
131
132
    /**
133
     * Setup application theme.
134
     *
135
     * @return  void
136
     */
137
    protected function _setTheme()
138
    {
139
        $theme = $this->request->getParam('theme');
140
        if ($theme) {
141
            $this->viewBuilder()->setTheme($theme);
142
        }
143
    }
144
}
145