Completed
Push — master ( 82dfad...f66689 )
by resu
02:51
created

AdminController::actionUpdateProfile()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
ccs 0
cts 18
cp 0
rs 8.5806
cc 4
eloc 18
nc 6
nop 1
crap 20
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Controller;
13
14
use Da\User\Event\UserEvent;
15
use Da\User\Factory\MailFactory;
16
use Da\User\Filter\AccessRuleFilter;
17
use Da\User\Model\Profile;
18
use Da\User\Model\User;
19
use Da\User\Query\UserQuery;
20
use Da\User\Search\UserSearch;
21
use Da\User\Service\UserBlockService;
22
use Da\User\Service\UserConfirmationService;
23
use Da\User\Service\UserCreateService;
24
use Da\User\Traits\ContainerAwareTrait;
25
use Da\User\Validator\AjaxRequestModelValidator;
26
use Yii;
27
use yii\base\Module;
28
use yii\db\ActiveRecord;
29
use yii\filters\AccessControl;
30
use yii\filters\VerbFilter;
31
use yii\helpers\Url;
32
use yii\web\Controller;
33
34
class AdminController extends Controller
35
{
36
    use ContainerAwareTrait;
37
38
    /**
39
     * @var UserQuery
40
     */
41
    protected $userQuery;
42
43
    /**
44
     * AdminController constructor.
45
     *
46
     * @param string    $id
47
     * @param Module    $module
48
     * @param UserQuery $userQuery
49
     * @param array     $config
50
     */
51 2
    public function __construct($id, Module $module, UserQuery $userQuery, array $config = [])
52
    {
53 2
        $this->userQuery = $userQuery;
54 2
        parent::__construct($id, $module, $config);
55 2
    }
56
57
    /**
58
     * @param \yii\base\Action $action
59
     *
60
     * @return bool
61
     */
62 2
    public function beforeAction($action)
63
    {
64 2
        if (in_array($action->id, ['index', 'update', 'update-profile', 'info', 'assignments'], true)) {
65 2
            Url::remember('', 'actions-redirect');
66
        }
67
68 2
        return parent::beforeAction($action);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 2
    public function behaviors()
75
    {
76
        return [
77
            'verbs' => [
78
                'class' => VerbFilter::class,
79
                'actions' => [
80
                    'delete' => ['post'],
81
                    'confirm' => ['post'],
82
                    'block' => ['post'],
83
                ],
84 2
            ],
85
            'access' => [
86
                'class' => AccessControl::class,
87
                'ruleConfig' => [
88
                    'class' => AccessRuleFilter::class,
89
                ],
90
                'rules' => [
91
                    [
92
                        'allow' => true,
93
                        'roles' => ['admin'],
94
                    ],
95
                ],
96
            ],
97
        ];
98
    }
99
100
    public function actionIndex()
101
    {
102
        $searchModel = $this->make(UserSearch::class);
103
        $dataProvider = $searchModel->search(Yii::$app->request->get());
104
105
        return $this->render(
106
            'index',
107
            [
108
                'dataProvider' => $dataProvider,
109
                'searchModel' => $searchModel,
110
            ]
111
        );
112
    }
113
114 1
    public function actionCreate()
115
    {
116
        /** @var User $user */
117 1
        $user = $this->make(User::class, [], ['scenario' => 'create']);
118
119
        /** @var UserEvent $event */
120 1
        $event = $this->make(UserEvent::class, [$user]);
121
122 1
        $this->make(AjaxRequestModelValidator::class, [$user])->validate();
123
124 1
        if ($user->load(Yii::$app->request->post())) {
125 1
            $this->trigger(UserEvent::EVENT_BEFORE_CREATE, $event);
126
127 1
            $mailService = MailFactory::makeWelcomeMailerService($user);
128
129 1
            if ($this->make(UserCreateService::class, [$user, $mailService])->run()) {
130 1
                Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User has been created'));
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...
131 1
                $this->trigger(UserEvent::EVENT_AFTER_CREATE, $event);
132
133 1
                return $this->redirect(['update', 'id' => $user->id]);
134
            }
135
        }
136
137 1
        return $this->render('create', ['user' => $user]);
138
    }
139
140 2
    public function actionUpdate($id)
141
    {
142 2
        $user = $this->userQuery->where(['id' => $id])->one();
143 2
        $user->setScenario('update');
144
        /** @var UserEvent $event */
145 2
        $event = $this->make(UserEvent::class, [$user]);
146
147 2
        $this->make(AjaxRequestModelValidator::class, [$user])->validate();
148
149 2
        if ($user->load(Yii::$app->request->post())) {
150 1
            $this->trigger(ActiveRecord::EVENT_BEFORE_UPDATE, $event);
151
152 1
            if ($user->save()) {
153 1
                Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Account details have been updated'));
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...
154 1
                $this->trigger(ActiveRecord::EVENT_AFTER_UPDATE, $event);
155
156 1
                return $this->refresh();
157
            }
158
        }
159
160 2
        return $this->render('_account', ['user' => $user]);
161
    }
162
163
    public function actionUpdateProfile($id)
164
    {
165
        /** @var User $user */
166
        $user = $this->userQuery->where(['id' => $id])->one();
167
        $profile = $user->profile;
168
        if ($profile === null) {
169
            $profile = $this->make(Profile::class);
170
            $profile->link($user);
171
        }
172
        /** @var UserEvent $event */
173
        $event = $this->make(UserEvent::class, [$user]);
174
175
        $this->make(AjaxRequestModelValidator::class, [$user])->validate();
176
177
        if ($profile->load(Yii::$app->request->post())) {
178
            if ($profile->save()) {
179
                $this->trigger(UserEvent::EVENT_BEFORE_PROFILE_UPDATE, $event);
180
                Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Profile details have been updated'));
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...
181
                $this->trigger(UserEvent::EVENT_AFTER_PROFILE_UPDATE, $event);
182
183
                return $this->refresh();
184
            }
185
        }
186
187
        return $this->render(
188
            '_profile',
189
            [
190
                'user' => $user,
191
                'profile' => $profile,
192
            ]
193
        );
194
    }
195
196
    public function actionInfo($id)
197
    {
198
        /** @var User $user */
199
        $user = $this->userQuery->where(['id' => $id])->one();
200
201
        return $this->render(
202
            '_info',
203
            [
204
                'user' => $user,
205
            ]
206
        );
207
    }
208
209
    public function actionAssignments($id)
210
    {
211
        /** @var User $user */
212
        $user = $this->userQuery->where(['id' => $id])->one();
213
214
        return $this->render(
215
            '_assignments',
216
            [
217
                'user' => $user,
218
                'params' => Yii::$app->request->post(),
219
            ]
220
        );
221
    }
222
223
    public function actionConfirm($id)
224
    {
225
        /** @var User $user */
226
        $user = $this->userQuery->where(['id' => $id])->one();
227
        /** @var UserEvent $event */
228
        $event = $this->make(UserEvent::class, [$user]);
229
230
        $this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event);
231
232
        if ($this->make(UserConfirmationService::class, [$user])->run()) {
233
            Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User has been confirmed'));
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...
234
            $this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event);
235
        } else {
236
            Yii::$app->getSession()->setFlash('warning', Yii::t('user', 'Unable to confirm user. Please, try again.'));
237
        }
238
239
        return $this->redirect(Url::previous('actions-redirect'));
240
    }
241
242
    public function actionDelete($id)
243
    {
244
        if ($id === Yii::$app->user->getId()) {
245
            Yii::$app->getSession()->setFlash('danger', Yii::t('user', 'You cannot remove your own account'));
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...
246
        } else {
247
            /** @var User $user */
248
            $user = $this->userQuery->where(['id' => $id])->one();
249
            /** @var UserEvent $event */
250
            $event = $this->make(UserEvent::class, [$user]);
251
            $this->trigger(ActiveRecord::EVENT_BEFORE_DELETE, $event);
252
253
            if ($user->delete()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user->delete() of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
254
                Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'User has been deleted'));
255
                $this->trigger(ActiveRecord::EVENT_AFTER_DELETE, $event);
256
            } else {
257
                Yii::$app->getSession()->setFlash(
258
                    'warning',
259
                    Yii::t('user', 'Unable to delete user. Please, try again later.')
260
                );
261
            }
262
        }
263
264
        return $this->redirect(['index']);
265
    }
266
267
    public function actionBlock($id)
268
    {
269
        if ($id === Yii::$app->user->getId()) {
270
            Yii::$app->getSession()->setFlash('danger', Yii::t('user', 'You cannot remove your own account'));
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...
271
        } else {
272
            /** @var User $user */
273
            $user = $this->userQuery->where(['id' => $id])->one();
274
            /** @var UserEvent $event */
275
            $event = $this->make(UserEvent::class, [$user]);
276
277
            if ($this->make(UserBlockService::class, [$user, $event, $this])->run()) {
278
                Yii::$app->getSession()->setFlash('success', Yii::t('user', 'User block status has been updated.'));
279
            } else {
280
                Yii::$app->getSession()->setFlash('danger', Yii::t('user', 'Unable to update block status.'));
281
            }
282
        }
283
284
        return $this->redirect(Url::previous('actions-redirect'));
285
    }
286
}
287