Completed
Push — master ( 5b5792...2b84bd )
by Schlaefer
03:33 queued 11s
created

JsDataHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 121
Duplicated Lines 7.44 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 9
loc 121
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeRender() 0 5 1
A getAppJs() 0 53 4
A _getCsrf() 0 11 2
A __call() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
                    'theme' => $View->getTheme(),
80
                    'apiroot' => $request->getAttribute('webroot') . 'api/v2/',
81
                    'webroot' => $request->getAttribute('webroot')
82
                ]
83
            ],
84
            'request' => [
85
                'action' => $request->getParam('action'),
86
                'controller' => $request->getParam('controller'),
87
                'isMobile' => $request->isMobile(),
88
                'isPreview' => $request->isPreview(),
89
                'csrf' => $this->_getCsrf($View)
90
            ],
91
            'currentUser' => [
92
                'id' => (int)$CurrentUser->get('id'),
93
                'username' => $CurrentUser->get('username'),
94
                'user_show_inline' => $CurrentUser->get('inline_view_on_click') || false,
95
                'user_show_thread_collapsed' => $CurrentUser->get('user_show_thread_collapsed') || false
96
            ],
97
            'callbacks' => [
98
                'beforeAppInit' => [],
99
                'afterAppInit' => [],
100
                'afterViewInit' => []
101
            ]
102
        ];
103
        $out = 'var SaitoApp = ' . json_encode($js);
104
        $out .= '; SaitoApp.timeAppStart = new Date().getTime();';
105
106
        return $out;
107
    }
108
109
    /**
110
     * Get CSRF-config
111
     *
112
     * @param View $View View
113
     * @return array
114
     * - 'header' HTTP header for CSRF-token
115
     * - 'token' CSRF-token
116
     */
117
    protected function _getCsrf(View $View)
118
    {
119
        $key = 'csrfToken';
120
        $token = $View->getRequest()->getCookie($key);
121
        if (empty($token)) {
122
            $token = $View->getResponse()->getCookie($key)['value'];
123
        }
124
        $header = 'X-CSRF-Token';
125
126
        return compact('header', 'token');
127
    }
128
129
    /**
130
     * Passes method calls on to JsData
131
     *
132
     * {@inheritDoc}
133
     */
134 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...
135
    {
136
        $proxy = [$this->_JsData, $method];
137
        if (is_callable($proxy)) {
138
            return call_user_func_array($proxy, $params);
139
        }
140
141
        parent::__call($method, $params);
142
    }
143
}
144