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

GoogleFeed::__set()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
rs 9.2
cc 4
eloc 9
nc 6
nop 2
1
<?php
2
3
namespace app\modules\shop\models;
4
5
6
use app\modules\config\components\ConfigurationSaveEvent;
7
use app\modules\config\helpers\ConfigurationUpdater;
8
use app\modules\config\models\Configurable;
9
use app\modules\shop\ShopModule;
10
use yii;
11
use yii\base\Model;
12
13
class GoogleFeed extends Model
14
{
15
    /** @property array $attrStorage */
16
    private $attrStorage = [];
17
18
    /**
19
     * @inheritdoc
20
     */
21
    public function rules()
22
    {
23
        $rules = [
24
            [
25
                [
26
                    'shop_host',
27
                    'shop_name',
28
                    'shop_description',
29
                    'shop_main_currency',
30
                    'shop_delivery_price',
31
                    'item_delivery_cost',
32
                    'feed_file_name',
33
                    'feed_handlers'
34
                ],
35
                'required'
36
            ],
37
            [['item_delivery_cost'], 'integer'],
38
            [['shop_name',], 'string', 'length' => [1, 255]],
39
            [$this->getOfferElements(), 'safe']
40
        ];
41
42
        return $rules;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function attributes()
49
    {
50
        return array_keys($this->attrStorage);
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function attributeLabels()
57
    {
58
        return [
59
            'shop_host' => Yii::t('app', 'Shop host'),
60
            'shop_title' => Yii::t('app', 'Shop title'),
61
            'shop_description' => Yii::t('app', 'Shop description'),
62
            'shop_main_currency' => Yii::t('app', 'Main currency'),
63
            'shop_delivery_price' => Yii::t('app', 'Shop delivery price'),
64
            'item_price' => Yii::t('app', 'Price'),
65
            'item_condition' => Yii::t('app', 'Condition'),
66
            'item_title' => Yii::t('app', 'Title'),
67
            'item_description' => Yii::t('app', 'Description'),
68
            'feed_handlers' => Yii::t('app', 'Handlers'),
69
            'feed_file_name' => Yii::t('app', 'File Name'),
70
            'item_gtin' => Yii::t('app', 'Google gtin'),
71
            'item_brand' => Yii::t('app', 'Brand'),
72
        ];
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function scenarios()
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...
79
    {
80
        return parent::scenarios();
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function init()
87
    {
88
        parent::init();
89
90
        $this->attrStorage = [
91
            'shop_host' => 'http://localhost.dev',
92
            'shop_name' => 'shop name',
93
            'shop_description' => 'shop description',
94
            'shop_delivery_price' => 'GB:Standard:100 GBP',
95
            'shop_main_currency' => 0,
96
            'item_price' => ['type' => 'field', 'key' => 'price'],
97
            'item_condition' => 'new',
98
            'item_title' => ['type' => 'field', 'key' => 'name'],
99
            'item_description' => ['type' => 'field', 'key' => 'announce'],
100
            'item_gtin' => ['type' => 'field', 'key' => 'sku'],
101
            'item_google_product_category' => ['type' => 'field'],
102
            'item_mpn' => ['type' => 'field'],
103
            'item_brand' => ['type' => 'field', 'key' => 'name'],
104
            'item_delivery_cost' => 0,
105
            'feed_handlers' => yii\helpers\Json::encode([
106
                'app\modules\shop\components\GoogleMerchants\DefaultHandler'
107
            ]),
108
            'feed_file_name' => 'feed.xml',
109
        ];
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 View Code Duplication
    public function __get($name)
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...
116
    {
117
        if (isset($this->attrStorage[$name])) {
118
            return $this->attrStorage[$name];
119
        }
120
        if (in_array($name, $this->attributes())) {
121
            return '';
122
        }
123
        return parent::__get($name);
124
    }
125
126
    /**
127
     * @inheritdoc
128
     */
129 View Code Duplication
    public function __set($name, $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...
130
    {
131
        if (is_array($value)) {
132
            $value = array_map('trim', $value);
133
        } elseif (is_string($value)) {
134
            $value = trim($value);
135
        }
136
137
        if (isset($this->attrStorage[$name])) {
138
            $this->attrStorage[$name] = $value;
139
            return true;
140
        }
141
142
        return parent::__set($name, $value);
143
    }
144
145
    /**
146
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

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...
147
     */
148
    public function getOfferElements()
149
    {
150
        return [
151
            'item_price',
152
            'item_title',
153
            'item_description',
154
            'item_gtin',
155
            'item_mpn',
156
            'item_google_product_category',
157
            'item_brand'
158
        ];
159
    }
160
161
    /**
162
     * @return bool
163
     */
164 View Code Duplication
    public function saveConfig()
165
    {
166
        /** @var ShopModule $module */
167
        if (null === $module = Yii::$app->getModule('shop')) {
168
            return false;
169
        }
170
        /** @var Configurable $configurable */
171
        if (null === $configurable = Configurable::findOne(['module' => $module->id])) {
172
            return false;
173
        }
174
        /** @var ConfigConfigurationModel $configurableModel */
175
        $configurableModel = $configurable->getConfigurableModel();
176
        $config = $this->attrStorage;
177
178
        yii\base\Event::on(
179
            $configurableModel::className(),
180
            $configurableModel->configurationSaveEvent(),
181
            function (ConfigurationSaveEvent $event) use ($config) {
182
                /** @var ConfigConfigurationModel $model */
183
                $model = $event->configurableModel;
184
                $model->googleFeedConfig = $config;
185
            }
186
        );
187
188
        $models = [$configurable];
189
        return ConfigurationUpdater::updateConfiguration($models, false);
190
    }
191
192
    /**
193
     * @return bool
194
     */
195 View Code Duplication
    public function loadConfig()
196
    {
197
        /** @var ShopModule $module */
198
        if (null === $module = Yii::$app->getModule('shop')) {
199
            return false;
200
        }
201
        if (empty($module->googleFeedConfig)) {
202
            return false;
203
        }
204
        $this->attrStorage = $module->googleFeedConfig;
205
206
        return static::validate();
207
    }
208
}