Completed
Pull Request — master (#178)
by Fèvre
03:02
created

AppController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace App\Controller;
3
4
use App\Event\Badges;
5
use App\I18n\Language;
6
use Cake\Controller\Controller;
7
use Cake\Event\Event;
8
use Cake\I18n\Time;
9
10
class AppController extends Controller
11
{
12
13
    /**
14
     * Initialization hook method.
15
     *
16
     * @return void
17
     */
18
    public function initialize()
19
    {
20
        parent::initialize();
21
22
        //$this->loadComponent('Flash');
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
23
    }
24
25
    /**
26
     * Components.
27
     *
28
     * @var array
29
     */
30
    public $components = [
31
        'Flash',
32
        'Cookie',
33
        'Acl.Acl',
34
        'SessionsActivity',
35
    /**
36
     * If you want enable CSRF uncomment this.
37
     * I recommend to enable it. If i have disable it, it's because
38
     * CloudFlare have some problem with the header X-CSRF-Token (AJAX Request).
39
     */
40
        /*'Csrf' => [
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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
            'secure' => true
42
        ],*/
43
        'Auth' => [
44
            'className' => 'AclAuth',
45
            'allowedActionsForBanned' => [
46
                'Pages' => [
47
                    'home'
48
                ]
49
            ],
50
            'authenticate' => [
51
                'Form',
52
                'Xety/Cake3CookieAuth.Cookie'
53
            ],
54
            'flash' => [
55
                'element' => 'error',
56
                'key' => 'flash',
57
                'params' => [
58
                    'class' => 'error'
59
                ]
60
            ],
61
            'authorize' => [
62
                'Acl.Actions' => [
63
                    'actionPath' => 'app/'
64
                ]
65
            ],
66
            'loginAction' => [
67
                'controller' => 'users',
68
                'action' => 'login',
69
                'prefix' => false
70
            ],
71
            'unauthorizedRedirect' => [
72
                'controller' => 'pages',
73
                'action' => 'home',
74
                'prefix' => false
75
            ],
76
            'loginRedirect' => [
77
                'controller' => 'pages',
78
                'action' => 'home'
79
            ],
80
            'logoutRedirect' => [
81
                'controller' => 'pages',
82
                'action' => 'home'
83
            ],
84
            'authError' => 'You are not authorized to access that location !'
85
        ]
86
    ];
87
88
    /**
89
     * Helpers.
90
     *
91
     * @var array
92
     */
93
    public $helpers = [
94
        'Form' => [
95
            'templates' => 'form-templates'
96
        ],
97
        'Paginator' => [
98
            'templates' => 'paginator-templates'
99
        ],
100
        'Acl'
101
    ];
102
103
    /**
104
     * beforeFilter handle.
105
     *
106
     * @param Event $event The beforeFilter event that was fired.
107
     *
108
     * @return void
109
     */
110
    public function beforeFilter(Event $event)
111
    {
112
        //Define the language.
113
        $language = new Language($this);
114
        $language->setLanguage();
115
116
        //Check for the Premium.
117
        $premium = $this->request->session()->read('Premium.Check') ? $this->request->session()->read('Premium.Check') : null;
118
        if (!is_null($premium)) {
119
            $this->loadModel('PremiumTransactions');
120
121
            $transaction = $this->PremiumTransactions
0 ignored issues
show
Documentation introduced by
The property PremiumTransactions does not exist on object<App\Controller\AppController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
122
                ->find()
123
                ->where([
124
                    'txn' => $this->request->session()->read('Premium.Check'),
125
                    'user_id' => $this->request->session()->read('Auth.User.id')
126
                ])
127
                ->contain(['Users'])
128
                ->first();
129
130
            if ($transaction) {
131
                //Write in the session the virtual field.
132
                $this->Auth->setUser($transaction->user->toArray());
133
                $this->request->session()->write('Auth.User.premium', $transaction->user->premium);
134
135
                $this->request->session()->delete('Premium.Check');
136
            }
137
        }
138
139
        //Set trustProxy or get the original visitor IP.
140
        $this->request->trustProxy = true;
141
142
        //Automatically Login.
143
        if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) {
144
            $this->loadModel('Users');
145
146
            $user = $this->Auth->identify();
147
            if ($user && $user['is_deleted'] == false) {
148
                $this->Auth->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->Auth->identify() on line 146 can also be of type boolean; however, Cake\Controller\Component\AuthComponent::setUser() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
149
150
                $user = $this->Users->newEntity($user, ['accessibleFields' => ['id' => true]]);
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\AppController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
151
                $user->isNew(false);
152
153
                $user->last_login = new Time();
154
                $user->last_login_ip = $this->request->clientIp();
155
156
                $this->Users->save($user);
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\AppController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
157
158
                //Write in the session the virtual field.
159
                $this->request->session()->write('Auth.User.premium', $user->premium);
160
161
                //Event.
162
                $this->eventManager()->attach(new Badges($this));
0 ignored issues
show
Documentation introduced by
new \App\Event\Badges($this) is of type object<App\Event\Badges>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

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...
163
164
                $user = new Event('Model.Users.register', $this, [
165
                    'user' => $user
166
                ]);
167
                $this->eventManager()->dispatch($user);
168
            } else {
169
                $this->Cookie->delete('CookieAuth');
170
            }
171
        }
172
173
        if (isset($this->request->params['prefix'])) {
174
            $prefix = explode('/', $this->request->params['prefix'])[0];
175
176
            switch ($prefix) {
177
                case 'admin':
178
                    $this->viewBuilder()->layout('admin');
179
                    break;
180
            }
181
        }
182
183
        $allowCookies = $this->Cookie->check('allowCookies');
184
        $this->set(compact('allowCookies'));
185
186
        //JavaScript Notifications.
187
        if ($this->request->session()->read('Notification') && !empty($this->request->session()->read('Notification'))) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->request->session()->read('Notification') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
188
            $notification = $this->request->session()->read('Notification');
189
            $this->request->session()->delete('Notification');
190
191
            $this->set(compact('notification'));
192
        }
193
    }
194
}
195