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 ( 6d1362...afb872 )
by Ivan
48:09 queued 28:53
created

YandexEcommerceHandler   C

Complexity

Total Complexity 19

Size/Duplication

Total Lines 283
Duplicated Lines 76.68 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 19
dl 217
loc 283
rs 6.875
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A installHandlers() 57 57 4
B handleCartAdd() 27 27 1
B handleRemoveFromCart() 27 27 1
B handleChangeQuantity() 27 27 1
B handleClearCart() 27 27 1
B handleProductShow() 33 33 5
A handleCartIndex() 0 3 1
B handlePurchase() 9 30 3
A getCategories() 10 10 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
namespace app\modules\seo\handlers;
3
4
use app\components\Controller;
5
use app\modules\core\events\ViewEvent;
6
use app\modules\seo\assets\YandexAnalyticsAssets;
7
use app\modules\shop\controllers\CartController;
8
use app\modules\shop\events\CartActionEvent;
9
use app\modules\shop\helpers\CurrencyHelper;
10
use app\modules\shop\models\Currency;
11
use app\modules\shop\models\Order;
12
use app\modules\shop\models\Product;
13
use yii\base\ActionEvent;
14
use yii\base\Event;
15
use yii\base\BaseObject;
16
use yii\helpers\Json;
17
use yii\web\View;
18
19
class YandexEcommerceHandler extends BaseObject
20
{
21
    /** @var Currency $currency */
22
    static protected $currency = null;
23
24
    /**
25
     * Install handlers
26
     */
27 View Code Duplication
    static public function installHandlers(ActionEvent $event)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
28
    {
29
        $currency = \Yii::$app->getModule('seo')->analytics['ecYandex']['currency'];
30
        if (AnalyticsHandler::CURRENCY_MAIN === intval($currency)) {
31
            static::$currency = CurrencyHelper::getMainCurrency();
32
        } elseif (AnalyticsHandler::CURRENCY_USER === intval($currency)) {
33
            static::$currency = CurrencyHelper::getUserCurrency();
34
        } else {
35
            static::$currency = CurrencyHelper::findCurrencyByIso($currency);
36
        }
37
38
        $route = implode('/', [
39
            $event->action->controller->module->id,
40
            $event->action->controller->id,
41
            $event->action->id
42
        ]);
43
44
        Event::on(
45
            CartController::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` 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...
46
            CartController::EVENT_ACTION_ADD,
47
            [self::className(), 'handleCartAdd'],
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` 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...
48
            false
49
        );
50
51
        Event::on(
52
            CartController::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` 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...
53
            CartController::EVENT_ACTION_REMOVE,
54
            [self::className(), 'handleRemoveFromCart'],
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` 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...
55
            false
56
        );
57
58
        Event::on(
59
            CartController::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` 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...
60
            CartController::EVENT_ACTION_QUANTITY,
61
            [self::className(), 'handleChangeQuantity'],
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` 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...
62
            false
63
        );
64
65
        Event::on(
66
            CartController::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` 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...
67
            CartController::EVENT_ACTION_CLEAR,
68
            [self::className(), 'handleClearCart'],
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` 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...
69
            false
70
        );
71
72
        Event::on(
73
            Controller::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` 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...
74
            Controller::EVENT_PRE_DECORATOR,
75
            [self::className(), 'handleProductShow']
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` 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...
76
        );
77
78
        if ('shop/cart/index' === $route) {
79
            self::handleCartIndex();
80
        }
81
82
        YandexAnalyticsAssets::register(\Yii::$app->getView());
83
    }
84
85
    /**
86
     * @param CartActionEvent $event
87
     */
88 View Code Duplication
    static public function handleCartAdd(CartActionEvent $event)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
89
    {
90
        $result = $event->getEventData();
91
        /** @var Currency $currency */
92
        $currency = static::$currency;
93
94
        $ya = [];
95
96
        $ya['currency'] = $currency->iso_code;
97
        $ya['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) {
98
            $quantity = $item['quantity'];
99
            /** @var Product $item */
100
            $item = $item['model'];
101
102
            $res[] = [
103
                'id' => $item->id,
104
                'name' => $item->name,
105
                'category' => self::getCategories($item),
106
                'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency),
0 ignored issues
show
Bug introduced by
The property currency does not seem to exist. Did you mean currency_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
107
                'quantity' => $quantity,
108
            ];
109
            return $res;
110
        }, []);
111
112
        $result['ecYandex'] = $ya;
113
        $event->setEventData($result);
114
    }
115
116
    /**
117
     * @param CartActionEvent $event
118
     */
119 View Code Duplication
    static public function handleRemoveFromCart(CartActionEvent $event)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
120
    {
121
        $result = $event->getEventData();
122
        /** @var Currency $currency */
123
        $currency = static::$currency;
124
125
        $ya = [];
126
127
        $ya['currency'] = $currency->iso_code;
128
        $ya['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) {
129
            $quantity = $item['quantity'];
130
            /** @var Product $item */
131
            $item = $item['model'];
132
133
            $res[] = [
134
                'id' => $item->id,
135
                'name' => $item->name,
136
                'category' => self::getCategories($item),
137
                'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency),
0 ignored issues
show
Bug introduced by
The property currency does not seem to exist. Did you mean currency_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
138
                'quantity' => $quantity,
139
            ];
140
            return $res;
141
        }, []);
142
143
        $result['ecYandex'] = $ya;
144
        $event->setEventData($result);
145
    }
146
147
    /**
148
     * @param CartActionEvent $event
149
     */
150 View Code Duplication
    static public function handleChangeQuantity(CartActionEvent $event)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
151
    {
152
        $result = $event->getEventData();
153
        /** @var Currency $currency */
154
        $currency = static::$currency;
155
156
        $ya = [];
157
158
        $ya['currency'] = $currency->iso_code;
159
        $ya['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) {
160
            $quantity = $item['quantity'];
161
            /** @var Product $item */
162
            $item = $item['model'];
163
164
            $res[] = [
165
                'id' => $item->id,
166
                'name' => $item->name,
167
                'category' => self::getCategories($item),
168
                'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency),
0 ignored issues
show
Bug introduced by
The property currency does not seem to exist. Did you mean currency_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
169
                'quantity' => $quantity,
170
            ];
171
            return $res;
172
        }, []);
173
174
        $result['ecYandex'] = $ya;
175
        $event->setEventData($result);
176
    }
177
178
    /**
179
     * @param CartActionEvent $event
180
     */
181 View Code Duplication
    static public function handleClearCart(CartActionEvent $event)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
182
    {
183
        $result = $event->getEventData();
184
        /** @var Currency $currency */
185
        $currency = static::$currency;
186
187
        $ya = [];
188
189
        $ya['currency'] = $currency->iso_code;
190
        $ya['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) {
191
            $quantity = $item['quantity'];
192
            /** @var Product $item */
193
            $item = $item['model'];
194
195
            $res[] = [
196
                'id' => $item->id,
197
                'name' => $item->name,
198
                'category' => self::getCategories($item),
199
                'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency),
0 ignored issues
show
Bug introduced by
The property currency does not seem to exist. Did you mean currency_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
200
                'quantity' => $quantity,
201
            ];
202
            return $res;
203
        }, []);
204
205
        $result['ecYandex'] = $ya;
206
        $event->setEventData($result);
207
    }
208
209
    /**
210
     * @param ViewEvent $event
211
     */
212 View Code Duplication
    static public function handleProductShow(ViewEvent $event)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
213
    {
214
        if ('shop/product/show' !== trim(\Yii::$app->requestedRoute, '/')) {
215
            return ;
216
        }
217
218
        /** @var Product $model */
219
        $model = isset($event->params['model']) ? $event->params['model'] : null;
220
        if (false === $model instanceof Product) {
221
            return ;
222
        }
223
224
        /** @var Currency $currency */
225
        $currency = static::$currency;
226
227
        $ya = [
228
            'action' => 'detail',
229
            'currency' => $currency->iso_code,
230
            'products' => [
231
                [
232
                    'id' => $model->id,
233
                    'name' => $model->name,
234
                    'category' => self::getCategories($model),
235
                    'price' => CurrencyHelper::convertCurrencies($model->price, $model->currency, $currency),
0 ignored issues
show
Bug introduced by
The property currency does not seem to exist. Did you mean currency_id?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
236
                    'quantity' => null === $model->measure ? 1 : $model->measure->nominal,
237
                ]
238
            ]
239
        ];
240
241
        $js = 'window.DotPlantParams = window.DotPlantParams || {};';
242
        $js .= 'window.DotPlantParams.ecYandex = ' . Json::encode($ya) . ';';
243
        \Yii::$app->getView()->registerJs($js, View::POS_BEGIN);
244
    }
245
246
    /**
247
     *
248
     */
249
    static public function handleCartIndex()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
250
    {
251
    }
252
253
    /**
254
     *
255
     */
256
    static public function handlePurchase()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
257
    {
258
        if (null === $order = Order::getOrder()) {
259
            return ;
260
        }
261
262
        /** @var Currency $currency */
263
        $currency = static::$currency;
264
265
        $ya = [
266
            'action' => 'purchase',
267
            'currency' => $currency->iso_code,
268
            'orderId' => $order->id,
269
            'products' => [],
270
        ];
271
272 View Code Duplication
        foreach ($order->items as $item) {
273
            $ya['products'][] = [
274
                'id' => $item->product->id,
275
                'name' => $item->product->name,
276
                'category' => self::getCategories($item->product),
0 ignored issues
show
Bug introduced by
It seems like $item->product can also be of type object<app\properties\HasProperties>; however, app\modules\seo\handlers...andler::getCategories() does only seem to accept object<app\modules\shop\models\Product>, 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...
277
                'price' => CurrencyHelper::convertCurrencies($item->product->price, $item->product->currency, $currency),
278
                'quantity' => $item->quantity,
279
            ];
280
        }
281
282
        $js = 'window.DotPlantParams = window.DotPlantParams || {};';
283
        $js .= 'window.DotPlantParams.ecYandex = ' . Json::encode($ya) . ';';
284
        \Yii::$app->getView()->registerJs($js, View::POS_BEGIN);
285
    }
286
287
    /**
288
     * @param Product $model
289
     * @return string
290
     */
291 View Code Duplication
    static private function getCategories(Product $model)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
292
    {
293
        $categories = [];
294
        $category = $model->category;
295
        while (null !== $category) {
296
            array_unshift($categories, $category->name);
297
            $category = $category->parent;
298
        }
299
        return implode('/', array_slice($categories, 0, 5));
300
    }
301
}
302