AuthUserBehavior::init()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace yiicod\auth\controllers\behaviors;
4
5
/*
6
 * Auth behavior with event for controller action
7
 * @author Orlov Alexey <[email protected]>
8
 */
9
10
use Yii;
11
use yii\base\ActionEvent;
12
use yii\base\Behavior;
13
use yii\base\Event;
14
use yii\web\BadRequestHttpException;
15
use yiicod\auth\actions\webUser\LoginAction;
16
use yiicod\auth\actions\webUser\LogoutAction;
17
use yiicod\auth\actions\webUser\RequestPasswordResetAction;
18
use yiicod\auth\actions\webUser\ResetPasswordAction;
19
use yiicod\auth\actions\webUser\SignupAction;
20
use yiicod\auth\events\LoginEvent;
21
use yiicod\auth\events\RequestPasswordResetEvent;
22
use yiicod\auth\events\ResetPasswordEvent;
23
use yiicod\auth\events\SignupEvent;
24
25
class AuthUserBehavior extends Behavior
26
{
27
    /**
28
     * Assign all events
29
     */
30
    public function init()
31
    {
32
        // Signup
33
        Event::on(SignupAction::class, SignupAction::EVENT_BEFORE_SIGNUP, [$this, 'beforeSignup']);
34
        Event::on(SignupAction::class, SignupAction::EVENT_AFTER_SIGNUP, [$this, 'afterSignup']);
35
        Event::on(SignupAction::class, SignupAction::EVENT_ERROR_SIGNUP, [$this, 'errorSignup']);
36
37
        // Login
38
        Event::on(LoginAction::class, LoginAction::EVENT_BEFORE_LOGIN, [$this, 'beforeLogin']);
39
        Event::on(LoginAction::class, LoginAction::EVENT_AFTER_LOGIN, [$this, 'afterLogin']);
40
        Event::on(LoginAction::class, LoginAction::EVENT_ERROR_LOGIN, [$this, 'errorLogin']);
41
42
        // Logout
43
        Event::on(LogoutAction::class, LogoutAction::EVENT_BEFORE_LOGOUT, [$this, 'beforeLogout']);
44
        Event::on(LogoutAction::class, LogoutAction::EVENT_AFTER_LOGOUT, [$this, 'afterLogout']);
45
46
        // ResetPassword
47
        Event::on(ResetPasswordAction::class, ResetPasswordAction::EVENT_BEFORE_RESET_PASSWORD, [$this, 'beforeResetPassword']);
48
        Event::on(ResetPasswordAction::class, ResetPasswordAction::EVENT_AFTER_RESET_PASSWORD, [$this, 'afterResetPassword']);
49
        Event::on(ResetPasswordAction::class, ResetPasswordAction::EVENT_ERROR_RESET_PASSWORD, [$this, 'errorResetPassword']);
50
51
        // RequestPasswordReset
52
        Event::on(RequestPasswordResetAction::class, RequestPasswordResetAction::EVENT_BEFORE_REQUEST_PASSWORD_RESET, [$this, 'beforeRequestPasswordReset']);
53
        Event::on(RequestPasswordResetAction::class, RequestPasswordResetAction::EVENT_AFTER_REQUEST_PASSWORD_RESET, [$this, 'afterRequestPasswordReset']);
54
        Event::on(RequestPasswordResetAction::class, RequestPasswordResetAction::EVENT_ERROR_REQUEST_PASSWORD_RESET, [$this, 'errorRequestPasswordReset']);
55
    }
56
57
    /**
58
     * After login action event
59
     *
60
     * @param LoginEvent $event
61
     */
62
    public function afterLogin($event)
63
    {
64
        $event->action->controller->goHome();
0 ignored issues
show
Bug introduced by
The method goHome does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
65
66
        Yii::$app->end();
67
    }
68
69
    /**
70
     * After signup action event
71
     *
72
     * @param SignupEvent $event
73
     */
74
    public function afterSignUp($event)
75
    {
76
        if (Yii::$app->getUser()->login($event->user)) {
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
77
            $event->action->controller->goHome();
0 ignored issues
show
Bug introduced by
The method goHome does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
78
79
            Yii::$app->end();
80
        }
81
    }
82
83
    /**
84
     * After RequestPasswordReset action event
85
     *
86
     * @param RequestPasswordResetEvent $event
87
     */
88
    public function afterRequestPasswordReset($event)
89
    {
90
        $mailerViewPath = Yii::$app->mailer->viewPath;
91
92
        Yii::$app->mailer->viewPath = '@yiicod/yii2-auth/mail';
93
        Yii::$app->mailer->compose('passwordResetToken', ['action' => $event->action, 'user' => $event->model->findUser()])
94
            ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot'])
95
            ->setTo($event->model->email)
96
            ->setSubject('Password reset for ' . Yii::$app->name)
97
            ->send();
98
99
        Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
100
101
        Yii::$app->mailer->viewPath = $mailerViewPath;
102
103
        $event->action->controller->goHome();
0 ignored issues
show
Bug introduced by
The method goHome does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
104
105
        Yii::$app->end();
106
    }
107
108
    /**
109
     * After ResetPassword action event
110
     *
111
     * @param ResetPasswordEvent $event
112
     */
113
    public function afterResetPassword($event)
114
    {
115
        Yii::$app->getSession()->setFlash('success', 'New password was saved.');
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
116
117
        $event->action->controller->goHome();
0 ignored issues
show
Bug introduced by
The method goHome does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
118
119
        Yii::$app->end();
120
    }
121
122
    /**
123
     * Before login action event
124
     *
125
     * @param LoginEvent $event
126
     *
127
     * @return mixed
128
     */
129
    public function beforeLogin($event)
130
    {
131
        if (false === Yii::$app->user->isGuest) {
132
            $event->action->controller->goHome();
0 ignored issues
show
Bug introduced by
The method goHome does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
133
            Yii::$app->end();
134
        }
135
    }
136
137
    /**
138
     * Before signup action event
139
     *
140
     * @param SignupEvent $event
141
     */
142
    public function beforeSignup($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
143
    {
144
    }
145
146
    /**
147
     * Before RequestPasswordReset action event
148
     *
149
     * @param RequestPasswordResetEvent $event
150
     */
151
    public function beforeRequestPasswordReset($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
152
    {
153
    }
154
155
    /**
156
     * Before ResetPassword action event
157
     *
158
     * @param ResetPasswordEvent $event
159
     */
160
    public function beforeResetPassword($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
161
    {
162
    }
163
164
    /**
165
     * error ResetPassword action event
166
     *
167
     * @param ResetPasswordEvent $event
168
     *
169
     * @throws BadRequestHttpException
170
     */
171
    public function errorResetPassword($event)
172
    {
173
        throw new BadRequestHttpException($event->e->getMessage());
0 ignored issues
show
Documentation introduced by
The property e does not exist on object<yiicod\auth\events\ResetPasswordEvent>. 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...
174
    }
175
176
    /**
177
     * Error RequestPasswordReset action event
178
     *
179
     * @param RequestPasswordResetEvent $event
180
     */
181
    public function errorRequestPasswordReset($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
182
    {
183
        Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.');
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
184
    }
185
186
    /**
187
     * @param ActionEvent $event
188
     */
189
    public function afterLogout($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
190
    {
191
    }
192
193
    /**
194
     * @param ActionEvent $event
195
     */
196
    public function beforeLogout($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
197
    {
198
    }
199
200
    /**
201
     * @param LoginEvent $event
202
     */
203
    public function errorLogin($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
204
    {
205
    }
206
207
    /**
208
     * @param SignupEvent $event
209
     */
210
    public function errorSignup($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
211
    {
212
    }
213
}
214