GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 3732bd...eba618 )
by Ivan
10:11
created

WishlistController::actionPrice()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
rs 8.6737
cc 5
eloc 17
nc 7
nop 0
1
<?php
2
3
namespace app\modules\shop\controllers;
4
5
use yii\web\Controller;
6
use Yii;
7
use yii\web\Response;
8
use app\modules\shop\models\Wishlist;
9
use yii\web\NotFoundHttpException;
10
use yii\web\BadRequestHttpException;
11
use app\modules\shop\models\WishlistProduct;
12
13
class WishlistController extends Controller
14
{
15
    /**
16
     * @return string
17
     */
18
    public function actionIndex()
19
    {
20
        $wishlists = Wishlist::getWishlist((!Yii::$app->user->isGuest ? Yii::$app->user->id : 0), Yii::$app->session->get('wishlists', []));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
21
        return $this->render('index',
22
            [
23
                'wishlists' => $wishlists,
24
            ]
25
        );
26
    }
27
28
    /**
29
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,integer|boolean>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
30
     * @throws NotFoundHttpException
31
     */
32
    public function actionAdd()
33
    {
34
        if (true === Yii::$app->request->isAjax) {
35
            Yii::$app->response->format = Response::FORMAT_JSON;
36
            $itemId = Yii::$app->request->post('id');
37
            $user_id = !Yii::$app->user->isGuest ? Yii::$app->user->id : 0;
38
            $wishlist_ids = Yii::$app->session->get('wishlists', []);
39
40
            /** @var Wishlist $wishlist */
41
            if (0 != Yii::$app->request->post('wishlistId')) {
42
43
                if (null !== $wishlist = Wishlist::findOne([
44
                        'id' => Yii::$app->request->post('wishlistId'),
45
                        'user_id' => $user_id,
46
                    ])
47
                ) {
48
                    $wishlist->addToWishlist($itemId);
49
                    return $result[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
50
                        'items' => Wishlist::countItems($user_id, $wishlist_ids),
51
                        'isSuccess' => true,
52
                    ];
53
                }
54
                return $result[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
55
                    'items' => Wishlist::countItems($user_id, $wishlist_ids),
56
                    'errorMessage' => Yii::t('app', 'Failed to add items'),
57
                    'isSuccess' => false,
58
                ];
59
            } else {
60
                $wishlists = Wishlist::getWishlist($user_id, $wishlist_ids);
61
                if (empty($wishlists)) {
62 View Code Duplication
                    if (null !== $wishlist = Wishlist::createWishlist(Yii::$app->request->post('title'), $user_id, $wishlist_ids, true)) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
63
                        $wishlist->addToWishlist($itemId);
64
                    }
65
                    return $result[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
66
                        'items' => Wishlist::countItems($user_id, $wishlist_ids),
67
                        'isSuccess' => true,
68
                    ];
69
                } else {
70 View Code Duplication
                    if (null !== $wishlist = Wishlist::createWishlist(Yii::$app->request->post('title'), $user_id, $wishlist_ids, false)) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 139 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
71
                        $wishlist->addToWishlist($itemId);
72
                    }
73
                    return $result[] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
74
                        'items' => Wishlist::countItems($user_id, $wishlist_ids),
75
                        'isSuccess' => true,
76
                    ];
77
                }
78
            }
79
        }
80
        throw new NotFoundHttpException();
81
    }
82
83
    /**
84
     * @param $id
85
     * @return Response
86
     */
87 View Code Duplication
    public function actionDelete($id)
88
    {
89
        /** @var Wishlist $wishlist */
90
        if (null !== $wishlist = Wishlist::findOne([
91
                'id' => $id,
92
                'user_id' => !Yii::$app->user->isGuest ? Yii::$app->user->id : 0,
93
            ])
94
        ) {
95
            $wishlist->delete();
96
            Yii::$app->session->setFlash('success', Yii::t('app', 'Wishlist has been removed'));
97
            return $this->redirect('index');
98
        }
99
        Yii::$app->session->setFlash('warning', Yii::t('app', 'Failed to remove wishlist'));
100
        return $this->redirect('index');
101
    }
102
103
    /**
104
     * @param $id
105
     * @return Response
106
     */
107
    public function actionDefault($id)
108
    {
109
        if (Wishlist::setDefaultWishlist($id, (!Yii::$app->user->isGuest ? Yii::$app->user->id : 0), Yii::$app->session->get('wishlists', []))) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 145 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
110
            Yii::$app->session->setFlash('success', Yii::t('app', 'Default wishlist changed'));
111
            return $this->redirect('index');
112
        }
113
        Yii::$app->session->setFlash('warning', Yii::t('app', 'Failed to set default wishlist'));
114
        return $this->redirect('index');
115
    }
116
117
118
    /**
119
     * @return Response
120
     */
121
    public function actionCreate()
122
    {
123
        $wishlists = Wishlist::getWishlist((!Yii::$app->user->isGuest ? Yii::$app->user->id : 0), Yii::$app->session->get('wishlists', []));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
124
        if (null !== Wishlist::createWishlist(
125
                Yii::$app->request->post('title'),
126
                !Yii::$app->user->isGuest ? Yii::$app->user->id : 0,
127
                Yii::$app->session->get('wishlists', []),
128
                empty($wishlists) ? true : false)
129
        ) {
130
            Yii::$app->session->setFlash('success', Yii::t('app', 'Wishlist has been created'));
131
            return $this->redirect('index');
132
        }
133
        Yii::$app->session->setFlash('warning', Yii::t('app', 'Failed to create wishlist'));
134
        return $this->redirect('index');
135
    }
136
137
    /**
138
     * @return Response
139
     */
140
    public function actionRename()
141
    {
142
        /** @var Wishlist $wishlist */
143
        if (null !== $wishlist = Wishlist::findOne([
144
                'id' => Yii::$app->request->post('id'),
145
                'user_id' => !Yii::$app->user->isGuest ? Yii::$app->user->id : 0,
146
            ])
147
        ) {
148
            $wishlist->renameWishlist(Yii::$app->request->post('title'));
149
            Yii::$app->session->setFlash('success', Yii::t('app', 'Wishlist has been renamed'));
150
            return $this->redirect('index');
151
        }
152
        Yii::$app->session->setFlash('warning', Yii::t('app', 'Failed to rename wishlist'));
153
        return $this->redirect('index');
154
    }
155
156
    /**
157
     * @param $id
158
     * @param $wishlistId
159
     * @return Response
160
     */
161
    public function actionRemove($id, $wishlistId)
162
    {
163
        /** @var WishlistProduct $item */
164
        if (null !== $item = WishlistProduct::findOne([
165
                'wishlist_id' => $wishlistId,
166
                'product_id' => $id
167
            ])
168
        ) {
169
            $item->delete();
170
            Yii::$app->session->setFlash('success', Yii::t('app', 'Item removed from the list'));
171
            return $this->redirect('index');
172
        }
173
        Yii::$app->session->setFlash('warning', Yii::t('app', 'Failed to remove item'));
174
        return $this->redirect('index');
175
    }
176
177
    /**
178
     * @param $id
179
     * @return Response
180
     */
181 View Code Duplication
    public function actionClear($id)
182
    {
183
        /** @var Wishlist $wishlist */
184
        if (null !== $wishlist = Wishlist::findOne([
185
                'id' => $id,
186
                'user_id' => !Yii::$app->user->isGuest ? Yii::$app->user->id : 0,
187
            ])
188
        ) {
189
            if ($wishlist->clearWishlist()) {
190
                Yii::$app->session->setFlash('success', Yii::t('app', 'Wishlist has been cleared'));
191
                return $this->redirect('index');
192
            }
193
        }
194
        Yii::$app->session->setFlash('warning', Yii::t('app', 'Failed to clear wishlist'));
195
        return $this->redirect('index');
196
    }
197
198
    /**
199
     * @return Response
200
     * @throws BadRequestHttpException
201
     */
202
    public function actionMove()
203
    {
204
        $wishlistToId = Yii::$app->request->post('wishlistTo');
205
        $wishlistFromId = Yii::$app->request->post('wishlistFrom');
206
        $selections = Yii::$app->request->post('selection');
207
        $user_id = !Yii::$app->user->isGuest ? Yii::$app->user->id : 0;
208
209
        if (null === $wishlistToId || null === $wishlistFromId) {
210
            throw new BadRequestHttpException;
211
        }
212
        /** @var Wishlist $wishlistFrom */
213
        /** @var Wishlist $wishlistTo */
214
        if ((null !== $wishlistFrom = Wishlist::findOne([
215
                    'id' => $wishlistFromId,
216
                    'user_id' => $user_id,
217
                ])) &&
218
            (null !== $wishlistTo = Wishlist::findOne([
219
                    'id' => $wishlistToId,
220
                    'user_id' => $user_id,
221
                ]))
222
        ) {
223
224
            if (null === $selections) {
225
                foreach ($wishlistFrom->items as $item) {
226
                    $wishlistTo->addToWishlist($item->product_id);
227
                    $wishlistFrom->removeItem($item->product_id);
228
                }
229
            } else {
230
                foreach ($wishlistFrom->items as $item) {
231
                    if (in_array($item->product_id, $selections)) {
232
                        $wishlistTo->addToWishlist($item->product_id);
233
                        $wishlistFrom->removeItem($item->product_id);
234
                    }
235
                }
236
            }
237
            Yii::$app->session->setFlash('success', Yii::t('app', 'Items moved'));
238
            return $this->redirect('index');
239
        }
240
        Yii::$app->session->setFlash('warning', Yii::t('app', 'Failed to move items'));
241
        return $this->redirect('index');
242
    }
243
244
    /**
245
     * @return Response
246
     * @throws BadRequestHttpException
247
     */
248
    public function actionRemoveGroup()
249
    {
250
        $wishlistFromId = Yii::$app->request->post('wishlistFrom');
251
        if (null === $wishlistFromId) {
252
            throw new BadRequestHttpException;
253
        }
254
        /** @var Wishlist $wishlist */
255
        if (null !== $wishlist = Wishlist::findOne([
256
                'id' => $wishlistFromId,
257
                'user_id' => !Yii::$app->user->isGuest ? Yii::$app->user->id : 0,
258
            ])
259
        ) {
260
261
            if (null !== $selections = Yii::$app->request->post('selection')) {
262
                foreach ($wishlist->items as $item) {
263
                    if (in_array($item->product_id, $selections)) {
264
                        $wishlist->removeItem($item->product_id);
265
                    }
266
                }
267
                Yii::$app->session->setFlash('success', Yii::t('app', 'Items removed'));
268
                return $this->redirect('index');
269
            }
270
        }
271
        Yii::$app->session->setFlash('warning', Yii::t('app', 'Failed to move items'));
272
        return $this->redirect('index');
273
    }
274
275
    /**
276
     * @return string
277
     * @throws NotFoundHttpException
278
     * @throws BadRequestHttpException
279
     */
280
    public function actionPrice()
281
    {
282
        if (true === Yii::$app->request->isAjax) {
283
            Yii::$app->response->format = Response::FORMAT_JSON;
284
            $selections = Yii::$app->request->post('selections');
285
            $wishlistId = Yii::$app->request->post('wishlistId');
286
            $user_id = !Yii::$app->user->isGuest ? Yii::$app->user->id : 0;
287
            $wishlist_ids = Yii::$app->session->get('wishlists', []);
288
            if (null === $wishlistId) {
289
                throw new BadRequestHttpException;
290
            }
291
            if (null === $selections) {
292
                $price = Wishlist::getTotalPrice($user_id, $wishlist_ids, $wishlistId);
293
                $items = Wishlist::countItems($user_id, $wishlist_ids, $wishlistId);
294
            } else {
295
                $price = Wishlist::getTotalPrice($user_id, $wishlist_ids, $wishlistId, $selections);
296
                $items = count($selections);
297
            }
298
            return $result = '<span class="wishlist-count">' . $items . '</span> ' . Yii::t('app', '{n, plural, one{item} other{items}} in the amount of', ['n' => $items]) . ' <span class="wishlist-price">' . $price . '</span>';
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 228 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
299
        }
300
        throw new NotFoundHttpException();
301
    }
302
}
303