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

GoogleEcommerceHandler   C

Complexity

Total Complexity 18

Size/Duplication

Total Lines 276
Duplicated Lines 77.9 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 19
dl 215
loc 276
rs 6.875
c 0
b 0
f 0

8 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() 31 31 5
B handleCartIndex() 9 32 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\GoogleAnalyticsAssets;
7
use app\modules\shop\controllers\CartController;
8
use app\modules\shop\events\CartActionEvent;
9
use app\modules\shop\models\Currency;
10
use app\modules\shop\models\Order;
11
use app\modules\shop\models\OrderItem;
12
use app\modules\shop\models\Product;
13
use app\modules\shop\helpers\CurrencyHelper;
14
use yii\base\ActionEvent;
15
use yii\base\Event;
16
use yii\base\BaseObject;
17
use yii\helpers\Json;
18
use yii\web\View;
19
20
class GoogleEcommerceHandler extends BaseObject
21
{
22
    /** @var Currency $currency */
23
    static protected $currency = null;
24
25
    /**
26
     *
27
     */
28 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...
29
    {
30
        $currency = \Yii::$app->getModule('seo')->analytics['ecGoogle']['currency'];
31
        if (AnalyticsHandler::CURRENCY_MAIN === intval($currency)) {
32
            static::$currency = CurrencyHelper::getMainCurrency();
33
        } elseif (AnalyticsHandler::CURRENCY_USER === intval($currency)) {
34
            static::$currency = CurrencyHelper::getUserCurrency();
35
        } else {
36
            static::$currency = CurrencyHelper::findCurrencyByIso($currency);
37
        }
38
39
        $route = implode('/', [
40
            $event->action->controller->module->id,
41
            $event->action->controller->id,
42
            $event->action->id
43
        ]);
44
45
        Event::on(
46
            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...
47
            CartController::EVENT_ACTION_ADD,
48
            [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...
49
            false
50
        );
51
52
        Event::on(
53
            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...
54
            CartController::EVENT_ACTION_REMOVE,
55
            [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...
56
            false
57
        );
58
59
        Event::on(
60
            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...
61
            CartController::EVENT_ACTION_QUANTITY,
62
            [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...
63
            false
64
        );
65
66
        Event::on(
67
            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...
68
            CartController::EVENT_ACTION_CLEAR,
69
            [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...
70
            false
71
        );
72
73
        Event::on(
74
            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...
75
            Controller::EVENT_PRE_DECORATOR,
76
            [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...
77
        );
78
79
        if ('shop/cart/index' === $route) {
80
            self::handleCartIndex();
81
        }
82
83
        GoogleAnalyticsAssets::register(\Yii::$app->getView());
84
    }
85
86
    /**
87
     * @param CartActionEvent $event
88
     */
89 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...
90
    {
91
        $result = $event->getEventData();
92
        /** @var Currency $currency */
93
        $currency = static::$currency;
94
95
        $ga = [];
96
97
        $ga['currency'] = $currency->iso_code;
98
        $ga['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) {
99
            /** @var Product $item */
100
            $quantity = $item['quantity'];
101
            $item = $item['model'];
102
103
            $res[] = [
104
                'id' => $item->id,
105
                'name' => $item->name,
106
                'category' => self::getCategories($item),
107
                'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency),
108
                'quantity' => $quantity,
109
            ];
110
            return $res;
111
        }, []);
112
113
        $result['ecGoogle'] = $ga;
114
        $event->setEventData($result);
115
    }
116
117
    /**
118
     * @param CartActionEvent $event
119
     */
120 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...
121
    {
122
        $result = $event->getEventData();
123
        /** @var Currency $currency */
124
        $currency = static::$currency;
125
126
        $ga = [];
127
128
        $ga['currency'] = $currency->iso_code;
129
        $ga['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) {
130
            /** @var Product $item */
131
            $quantity = $item['quantity'];
132
            $item = $item['model'];
133
134
            $res[] = [
135
                'id' => $item->id,
136
                'name' => $item->name,
137
                'category' => self::getCategories($item),
138
                'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency),
139
                'quantity' => $quantity,
140
            ];
141
            return $res;
142
        }, []);
143
144
        $result['ecGoogle'] = $ga;
145
        $event->setEventData($result);
146
    }
147
148
    /**
149
     * @param CartActionEvent $event
150
     */
151 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...
152
    {
153
        $result = $event->getEventData();
154
        /** @var Currency $currency */
155
        $currency = static::$currency;
156
157
        $ga = [];
158
159
        $ga['currency'] = $currency->iso_code;
160
        $ga['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) {
161
            /** @var Product $item */
162
            $quantity = $item['quantity'];
163
            $item = $item['model'];
164
165
            $res[] = [
166
                'id' => $item->id,
167
                'name' => $item->name,
168
                'category' => self::getCategories($item),
169
                'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency),
170
                'quantity' => $quantity,
171
            ];
172
            return $res;
173
        }, []);
174
175
        $result['ecGoogle'] = $ga;
176
        $event->setEventData($result);
177
    }
178
179
    /**
180
     * @param CartActionEvent $event
181
     */
182 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...
183
    {
184
        $result = $event->getEventData();
185
        /** @var Currency $currency */
186
        $currency = static::$currency;
187
188
        $ga = [];
189
190
        $ga['currency'] = $currency->iso_code;
191
        $ga['products'] = array_reduce($event->getProducts(), function($res, $item) use ($currency) {
192
            /** @var Product $item */
193
            $quantity = $item['quantity'];
194
            $item = $item['model'];
195
196
            $res[] = [
197
                'id' => $item->id,
198
                'name' => $item->name,
199
                'category' => self::getCategories($item),
200
                'price' => CurrencyHelper::convertCurrencies($item->price, $item->currency, $currency),
201
                'quantity' => $quantity,
202
            ];
203
            return $res;
204
        }, []);
205
206
        $result['ecGoogle'] = $ga;
207
        $event->setEventData($result);
208
    }
209
210
    /**
211
     * @param ViewEvent $event
212
     */
213 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...
214
    {
215
        if ('shop/product/show' !== trim(\Yii::$app->requestedRoute, '/')) {
216
            return ;
217
        }
218
219
        /** @var Product $model */
220
        $model = isset($event->params['model']) ? $event->params['model'] : null;
221
        if (false === $model instanceof Product) {
222
            return ;
223
        }
224
225
        /** @var Currency $currency */
226
        $currency = static::$currency;
227
228
        $ga = [
229
            'action' => 'detail',
230
            'currency' => $currency->iso_code,
231
            'products' => [
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
        $js = 'window.DotPlantParams = window.DotPlantParams || {};';
241
        $js .= 'window.DotPlantParams.ecGoogle = ' . Json::encode($ga) . ';';
242
        \Yii::$app->getView()->registerJs($js, View::POS_BEGIN);
243
    }
244
245
    /**
246
     *
247
     */
248
    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...
249
    {
250
        if (null === $order = Order::getOrder()) {
251
            return ;
252
        }
253
254
        /** @var Currency $currency */
255
        $currency = static::$currency;
256
257
        $ga = [
258
            'action' => 'action',
259
            'type' => 'checkout',
260
            'step' => 1,
261
            'currency' => $currency->iso_code,
262
            'products' => [],
263
        ];
264
265 View Code Duplication
        foreach ($order->items as $item) {
266
            $ga['products'][] = [
267
                'id' => $item->product->id,
268
                'name' => $item->product->name,
269
                '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...
270
                'price' => CurrencyHelper::convertCurrencies($item->product->price, $item->product->currency, $currency),
271
                'quantity' => $item->quantity,
272
            ];
273
        }
274
275
276
        $js = 'window.DotPlantParams = window.DotPlantParams || {};';
277
        $js .= 'window.DotPlantParams.ecGoogle = ' . Json::encode($ga) . ';';
278
        \Yii::$app->getView()->registerJs($js, View::POS_BEGIN);
279
    }
280
281
    /**
282
     * @param Product $model
283
     * @return string
284
     */
285 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...
286
    {
287
        $categories = [];
288
        $category = $model->category;
289
        while (null !== $category) {
290
            array_unshift($categories, $category->name);
291
            $category = $category->parent;
292
        }
293
        return implode('/', array_slice($categories, 0, 5));
294
    }
295
}
296