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 — master ( e91cf6...3a6b3f )
by Alexander
10:43
created

GoogleMerchants::generateFeedByArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace app\modules\shop\components\GoogleMerchants;
4
5
use app\modules\shop\models\Currency;
6
use app\modules\shop\models\GoogleFeed;
7
use app\modules\shop\models\Product;
8
use Yii;
9
use yii\base\Component;
10
use yii\helpers\Html;
11
use yii\helpers\Json;
12
13
14
/**
15
 * Class GoogleMerchants
16
 * This implementation uses a rss 2.0 template
17
 * @package app\modules\shop\components\GoogleMerchants
18
 */
19
class GoogleMerchants extends Component
20
{
21
22
    protected static $modelSetting;
23
24
    public $host = null;
25
    public $title = null;
26
    public $description = null;
27
    public $fileName = null;
28
29
    public $handlers = [
30
        'app\modules\shop\components\GoogleMerchants\DefaultHandler'
31
    ];
32
    public $brand_property = 'manufacturer';
33
34
    public $mainCurrency = null;
35
    protected $data = [];
36
37
    const MODIFICATION_DATA = 'modification_data';
38
39
    public function init()
40
    {
41
        self::$modelSetting = new GoogleFeed();
42
        self::$modelSetting->loadConfig();
43
44
        $this->handlers = Json::decode(self::$modelSetting->feed_handlers);
0 ignored issues
show
Documentation introduced by
The property feed_handlers does not exist on object<app\modules\shop\models\GoogleFeed>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation Bug introduced by
It seems like \yii\helpers\Json::decod...Setting->feed_handlers) of type * is incompatible with the declared type array of property $handlers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
46
        foreach ($this->handlers as $handler) {
47
            if (is_subclass_of($handler, ModificationDataInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \app\modules\shop\compon...ionDataInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
48
                $this->on(self::MODIFICATION_DATA, [$handler, 'processData']);
49
            }
50
        }
51
        parent::init();
52
53
54
        if ($this->mainCurrency === null) {
55
            $this->mainCurrency = Currency::findOne(['iso_code' => self::$modelSetting->shop_main_currency]);
0 ignored issues
show
Documentation introduced by
The property shop_main_currency does not exist on object<app\modules\shop\models\GoogleFeed>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
        }
57
58
        if ($this->host === null) {
59
            $this->host = self::$modelSetting->shop_host;
0 ignored issues
show
Documentation introduced by
The property shop_host does not exist on object<app\modules\shop\models\GoogleFeed>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
        }
61
        if ($this->title === null) {
62
            $this->title = self::$modelSetting->shop_name;
0 ignored issues
show
Documentation introduced by
The property shop_name does not exist on object<app\modules\shop\models\GoogleFeed>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
        }
64
        if ($this->description === null) {
65
            $this->description = self::$modelSetting->shop_description;
0 ignored issues
show
Documentation introduced by
The property shop_description does not exist on object<app\modules\shop\models\GoogleFeed>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66
        }
67
        if ($this->fileName === null) {
68
            $this->fileName = self::$modelSetting->feed_file_name;
0 ignored issues
show
Documentation introduced by
The property feed_file_name does not exist on object<app\modules\shop\models\GoogleFeed>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
69
        }
70
71
    }
72
73
74
    public function saveFeedInFs()
75
    {
76
        file_put_contents(Yii::getAlias('@webroot/' . $this->fileName), $this->generateFeedByArray($this->getData()));
77
    }
78
79
    public function getData()
80
    {
81
        if ($this->data === []) {
82
            $event = new ModificationDataEvent();
83
            $query = Product::find()
84
                ->where(['active' => 1]);
85
86
            foreach ($query->each() as $product) {
87
                $event->model = $product;
88
                $this->trigger(self::MODIFICATION_DATA, $event);
89
                $this->data[] = $event->dataArray;
90
            }
91
        }
92
        return $this->data;
93
    }
94
95
    public function generateFeedByArray($data)
96
    {
97
        $result = "<?xml version=\"1.0\"?><rss xmlns:g=\"http://base.google.com/ns/1.0\" version=\"2.0\">";
98
        $result .= $this->generateItem(
99
            'channel',
100
            $this->generateItem('title', $this->title) .
101
            $this->generateItem('link', $this->host) .
102
            $this->generateItem('description', $this->description) .
103
            $this->generateItems($data)
104
        );
105
        $result .= "</rss>";
106
        return $result;
107
    }
108
109
    protected function generateItems($data)
110
    {
111
        $result = "";
112
        foreach ($data as $item) {
113
            $result .= $this->generateItem('item', $item);
114
        }
115
        return $result;
116
    }
117
118
    protected function generateItem($tag, $data)
119
    {
120
        $content = "";
121
        if (is_array($data)) {
122
            foreach ($data as $key => $value) {
123
                $content .= $this->generateItem($key, $value);
124
            }
125
        } else {
126
            $content = $data;
127
        }
128
        return static::tag($tag, $content);
129
    }
130
131
    protected static function tag($name, $content = '', $options = [])
132
    {
133
        return "<$name" . Html::renderTagAttributes($options) . '>' . $content . "</$name>";
134
    }
135
136
}
137