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

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
dl 0
loc 13
rs 9.4286
c 1
b 0
f 1
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);
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]);
56
        }
57
58
        if ($this->host === null) {
59
            $this->host = self::$modelSetting->shop_host;
60
        }
61
        if ($this->title === null) {
62
            $this->title = self::$modelSetting->shop_name;
63
        }
64
        if ($this->description === null) {
65
            $this->description = self::$modelSetting->shop_description;
66
        }
67
        if ($this->fileName === null) {
68
            $this->fileName = self::$modelSetting->feed_file_name;
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
                ->limit(3);
86
87
            foreach ($query->each() as $product) {
88
                $event->model = $product;
89
                $this->trigger(self::MODIFICATION_DATA, $event);
90
                $this->data[] = $event->dataArray;
91
            }
92
        }
93
        return $this->data;
94
    }
95
96
    public function generateFeedByArray($data)
97
    {
98
        $result = "<?xml version=\"1.0\"?><rss xmlns:g=\"http://base.google.com/ns/1.0\" version=\"2.0\">";
99
        $result .= $this->generateItem(
100
            'channel',
101
            $this->generateItem('title', $this->title) .
102
            $this->generateItem('link', $this->host) .
103
            $this->generateItem('description', $this->description) .
104
            $this->generateItems($data)
105
        );
106
        $result .= "</rss>";
107
        return $result;
108
    }
109
110
    protected function generateItems($data)
111
    {
112
        $result = "";
113
        foreach ($data as $item) {
114
            $result .= $this->generateItem('item', $item);
115
        }
116
        return $result;
117
    }
118
119
    protected function generateItem($tag, $data)
120
    {
121
        $content = "";
122
        if (is_array($data)) {
123
            foreach ($data as $key => $value) {
124
                $content .= $this->generateItem($key, $value);
125
            }
126
        } else {
127
            $content = $data;
128
        }
129
        return static::tag($tag, $content);
130
    }
131
132
    protected static function tag($name, $content = '', $options = [])
133
    {
134
        return "<$name" . Html::renderTagAttributes($options) . '>' . $content . "</$name>";
135
    }
136
137
}
138