Completed
Push — master ( 3c1969...5b5792 )
by Schlaefer
03:16 queued 10s
created

JsDataHelper::getAppJs()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 8
nop 2
dl 0
loc 55
rs 8.9818
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Saito - The Threaded Web Forum
4
 *
5
 * @copyright Copyright (c) the Saito Project Developers 2015
6
 * @link https://github.com/Schlaefer/Saito
7
 * @license http://opensource.org/licenses/MIT
8
 */
9
10
namespace App\View\Helper;
11
12
use App\Controller\Component\CurrentUserComponent;
13
use Cake\Core\Configure;
14
use Cake\Event\Event;
15
use Cake\View\Helper;
16
use Cake\View\View;
17
use Saito\JsData\JsData;
18
use Saito\User\ForumsUserInterface;
19
20
/**
21
 * Javascript Data Helper
22
 */
23
class JsDataHelper extends AppHelper
24
{
25
26
    public $helpers = ['Url'];
27
28
    /**
29
     * JsData
30
     *
31
     * @var JsData
32
     */
33
    protected $_JsData;
34
35
    /**
36
     * CakePHP beforeRender event-handler
37
     *
38
     * @param Event $event event
39
     * @param mixed $viewFile view file
40
     * @return void
41
     */
42
    public function beforeRender(Event $event, $viewFile): void
0 ignored issues
show
Unused Code introduced by
The parameter $viewFile 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...
43
    {
44
        $View = $event->getSubject();
45
        $this->_JsData = $View->get('jsData');
46
    }
47
48
    /**
49
     * get app js
50
     *
51
     * @param View $View view
52
     * @param ForumsUserInterface $CurrentUser user
53
     * @return string
54
     */
55
    public function getAppJs(View $View, ForumsUserInterface $CurrentUser)
56
    {
57
        $settings = Configure::read('Saito.Settings');
58
        $request = $View->getRequest();
59
60
        $js = $this->_JsData->getJs();
61
        $js += [
62
            'app' => [
63
                'version' => Configure::read('Saito.v'),
64
                'settings' => [
65
                    'autoPageReload' => (isset($View->viewVars['autoPageReload']) ? $View->viewVars['autoPageReload'] : 0),
0 ignored issues
show
Deprecated Code introduced by
The property Cake\View\ViewVarsTrait::$viewVars has been deprecated with message: 3.7.0 Use `$this->set()` instead, also see `$this->viewBuilder()->getVar()`.

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

Loading history...
66
                    'editPeriod' => (int)Configure::read(
67
                        'Saito.Settings.edit_period'
68
                    ),
69
                    'language' => Configure::read('Saito.language'),
70
                    'notificationIcon' => $this->Url->assetUrl(
71
                        'html5-notification-icon.png',
72
                        [
73
                            'pathPrefix' => Configure::read('App.imageBaseUrl'),
74
                            'fullBase' => true
75
                        ]
76
                    ),
77
                    'quote_symbol' => $settings['quote_symbol'],
78
                    'subject_maxlength' => $settings['subject_maxlength'],
79
                    'upload_max_img_size' => $settings['upload_max_img_size'] * 1024,
80
                    'upload_max_number_of_uploads' => (int)$settings['upload_max_number_of_uploads'],
81
                    'theme' => $View->getTheme(),
82
                    'apiroot' => $request->getAttribute('webroot') . 'api/v2/',
83
                    'webroot' => $request->getAttribute('webroot')
84
                ]
85
            ],
86
            'request' => [
87
                'action' => $request->getParam('action'),
88
                'controller' => $request->getParam('controller'),
89
                'isMobile' => $request->isMobile(),
90
                'isPreview' => $request->isPreview(),
91
                'csrf' => $this->_getCsrf($View)
92
            ],
93
            'currentUser' => [
94
                'id' => (int)$CurrentUser->get('id'),
95
                'username' => $CurrentUser->get('username'),
96
                'user_show_inline' => $CurrentUser->get('inline_view_on_click') || false,
97
                'user_show_thread_collapsed' => $CurrentUser->get('user_show_thread_collapsed') || false
98
            ],
99
            'callbacks' => [
100
                'beforeAppInit' => [],
101
                'afterAppInit' => [],
102
                'afterViewInit' => []
103
            ]
104
        ];
105
        $out = 'var SaitoApp = ' . json_encode($js);
106
        $out .= '; SaitoApp.timeAppStart = new Date().getTime();';
107
108
        return $out;
109
    }
110
111
    /**
112
     * Get CSRF-config
113
     *
114
     * @param View $View View
115
     * @return array
116
     * - 'header' HTTP header for CSRF-token
117
     * - 'token' CSRF-token
118
     */
119
    protected function _getCsrf(View $View)
120
    {
121
        $key = 'csrfToken';
122
        $token = $View->getRequest()->getCookie($key);
123
        if (empty($token)) {
124
            $token = $View->getResponse()->getCookie($key)['value'];
125
        }
126
        $header = 'X-CSRF-Token';
127
128
        return compact('header', 'token');
129
    }
130
131
    /**
132
     * Passes method calls on to JsData
133
     *
134
     * {@inheritDoc}
135
     */
136 View Code Duplication
    public function __call($method, $params)
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...
137
    {
138
        $proxy = [$this->_JsData, $method];
139
        if (is_callable($proxy)) {
140
            return call_user_func_array($proxy, $params);
141
        }
142
143
        parent::__call($method, $params);
144
    }
145
}
146