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.
Test Setup Failed
Push — filters ( b41112...24729b )
by
unknown
13:18
created

DefaultHandler::getPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 1
eloc 8
nc 1
nop 3
1
<?php
2
3
namespace app\modules\shop\components\GoogleMerchants;
4
5
6
use app\models\Property;
7
use app\modules\shop\helpers\CurrencyHelper;
8
use app\modules\shop\models\Category;
9
use app\modules\shop\models\GoogleFeed;
10
use app\modules\shop\models\Product;
11
use yii\console\Exception;
12
use yii\helpers\Url;
13
14
15
class DefaultHandler implements ModificationDataInterface
16
{
17
18
    protected static $breadcrumbsData = [];
19
20
    protected static $modelSetting = null;
21
22
23
    public static function processData(ModificationDataEvent $event)
24
    {
25
        if (!self::$modelSetting) {
26
            self::$modelSetting = new GoogleFeed();
27
            self::$modelSetting->loadConfig();
28
        }
29
        $event->dataArray = [
30
            'title' => self::getRelation($event->model, 'item_title', $event->model->name),
31
            'description' => self::getRelation($event->model, 'item_description', $event->model->announce),
32
            'link' => $event->sender->host . htmlspecialchars(Url::toRoute(['@product', 'model' => $event->model])),
33
            'g:id' => $event->model->id,
34
            'g:condition' => self::$modelSetting->item_condition,
35
            'g:product_type' => self::getProductType($event->model),
36
            'g:availability' => static::getAvailability($event->model),
37
            'g:shipping' => static::getShipping()
38
        ];
39
        if ($manufacturer = self::getRelation($event->model, 'item_brand', false)) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
            $event->dataArray['g:brand'] = htmlspecialchars($manufacturer);
41
        }
42
        if ($gin = self::getRelation($event->model, 'item_gtin', false)) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
            $event->dataArray['g:gtin'] = htmlspecialchars($gin);
44
        }
45
        if ($mpn = self::getRelation($event->model, 'item_mpn', false)) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
            $event->dataArray['g:mpn'] = htmlspecialchars($mpn);
47
        }
48
49
        if ($item_google_product_category = self::getRelation($event->model, 'item_google_product_category', false)) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
            $event->dataArray['g:google_product_category'] = htmlspecialchars($item_google_product_category);
51
        }
52
53
        if (!empty($event->model->old_price) && $event->model->old_price > $event->model->price) {
54
            $event->dataArray['g:price'] = static::getPrice($event->model,
55
                $event->sender->mainCurrency,
56
                $event->model->old_price
57
            );
58
            $event->dataArray['g:g:sale_price'] = static::getPrice($event->model,
59
                $event->sender->mainCurrency,
60
                $event->model->price
61
            );
62
        } else {
63
64
            $event->dataArray['g:price'] = static::getPrice($event->model,
65
                $event->sender->mainCurrency,
66
                $event->model->price
67
            );
68
        }
69
        $imageObject = $event->model->image;
70
        if ($imageObject) {
71
            $event->dataArray['g:image_link'] = htmlspecialchars(
72
                $event->sender->host . $event->model->image->getOriginalUrl()
73
            );
74
        }
75
        if ($event->model->parent_id !== 0) {
76
            $event->dataArray['g:item_group_id'] = $event->model->parent_id;
77
        }
78
    }
79
80
81
    protected static function getRelation($model, $relationName, $default_value = '')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
82
    {
83
        $result = $default_value;
84
        $relation = self::$modelSetting->$relationName;
85
86
        if ($relation['type'] === 'field' && !empty($relation['key'])) {
87
            try {
88
                $result = $model->{$relation['key']};
89
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class yii\console\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
90
            }
91
        } elseif ($relation['type'] === 'property' && !empty($relation['key'])) {
92
            try {
93
                $propertyKey = Property::find()->select('key')->where(['id' => $relation['key']])->asArray()->scalar();
94
                $result = $model->property($propertyKey);
95
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class yii\console\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
96
            }
97
        } elseif ($relation['type'] === 'relation' && !empty($relation['key']) && !empty($relation['value'])) {
98
            $relModel = $model->{$relation['key']}();
99
            $result = $relModel->$relation['value'];
100
        }
101
102
        return $result;
103
    }
104
105
106
    protected static function getProductType(Product $model)
107
    {
108
        if (!isset(self::$breadcrumbsData[$model->main_category_id])) {
109
            $parentIds = $model->getMainCategory()->getParentIds();
110
            $breadcrumbs = [];
111
            foreach ($parentIds as $id) {
112
                $breadcrumbs[] = Category::find()->select(['name'])->where(['id' => $id])->asArray()->scalar();
113
            }
114
            $breadcrumbs[] = $model->getMainCategory()->name;
115
            self::$breadcrumbsData[$model->main_category_id] = $breadcrumbs;
116
        }
117
        return htmlspecialchars(implode(' > ', self::$breadcrumbsData[$model->main_category_id]));
118
    }
119
120
    protected static function getPrice(Product $model, $mainCurrency, $price)
121
    {
122
        return number_format(
123
            CurrencyHelper::convertCurrencies($price, $model->currency,
124
                $mainCurrency),
125
            2,
126
            '.',
127
            ''
128
        ) . ' ' . $mainCurrency->iso_code;
129
    }
130
131
    protected static function getAvailability(Product $model)
132
    {
133
        if ($model->unlimited_count === 1) {
134
            $inStock = 'in stock';
135
        } else {
136
            $inStock = 'out of stock';
137
            foreach ($model->getWarehousesState() as $warehouse) {
138
                if ($warehouse->in_warehouse > 0) {
139
                    $inStock = 'in stock';
140
                    break;
141
                }
142
            }
143
        }
144
        return $inStock;
145
    }
146
147
    protected static function getShipping()
148
    {
149
        $data = explode(':', self::$modelSetting->shop_delivery_price);
150
151
        return [
152
            'g:country' => $data[0],
153
            'g:service' => $data[1],
154
            'g:price' => $data[2]
155
        ];
156
157
    }
158
159
160
}