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 — google-feed ( 39b9ea...5c4917 )
by
unknown
22:49
created

GoogleMerchants::saveFeedInFs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace app\modules\shop\components\GoogleMerchants;
4
5
use app\modules\shop\models\Currency;
6
use app\modules\shop\models\Product;
7
use Yii;
8
use yii\base\Component;
9
use yii\helpers\Html;
10
11
12
/**
13
 * Class GoogleMerchants
14
 * This implementation uses a rss 2.0 template
15
 * @package app\modules\shop\components\GoogleMerchants
16
 */
17
class GoogleMerchants extends Component
18
{
19
20
    public $host = 'https://localhost';
21
    public $title = 'Site title';
22
    public $description = 'Site description';
23
    public $fileName = 'feed.xml';
24
    public $mainCurrency = null;
25
    public $handlers = [
26
        'app\modules\shop\components\GoogleMerchants\DefaultHandler'
27
    ];
28
29
30
    protected $data = [];
31
32
    const MODIFICATION_DATA = 'modification_data';
33
34
    public function init()
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...
35
    {
36
        if ($this->mainCurrency === null) {
37
            $this->mainCurrency = Currency::getMainCurrency();
38
        }
39
        foreach ($this->handlers as $handler) {
40
            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...
41
                $this->on(self::MODIFICATION_DATA, [$handler, 'processData']);
42
            }
43
        }
44
        return parent::init();
45
    }
46
47
48
    public function saveFeedInFs()
49
    {
50
        file_put_contents(Yii::getAlias('@webroot/' . $this->fileName), $this->generateFeedByArray($this->getData()));
51
    }
52
53
    public function getData()
54
    {
55
        if ($this->data === []) {
56
            $event = new ModificationDataEvent();
57
            foreach (Product::find()->where(['active' => 1])->limit(2)->all() as $product) {
58
                $event->model = $product;
59
                $this->trigger(self::MODIFICATION_DATA, $event);
60
                $this->data[] = $event->data;
61
            }
62
        }
63
        return $this->data;
64
    }
65
66
    public function generateFeedByArray($data)
67
    {
68
        $result = "<?xml version=\"1.0\"?><rss xmlns:g=\"http://base.google.com/ns/1.0\" version=\"2.0\">";
69
        $result .= $this->generateItem(
70
            'channel',
71
            $this->generateItem('title', $this->title) .
72
            $this->generateItem('link', $this->host) .
73
            $this->generateItem('description', $this->description) .
74
            $this->generateItems($data)
75
        );
76
        $result .= "</rss>";
77
        return $result;
78
    }
79
80
    protected function generateItems($data)
81
    {
82
        $result = "";
83
        foreach ($data as $item) {
84
            $result .= $this->generateItem('item', $item);
85
        }
86
        return $result;
87
    }
88
89
    protected function generateItem($tag, $data)
90
    {
91
        $content = "";
92
        if (is_array($data)) {
93
            foreach ($data as $key => $value) {
94
                $content .= $this->generateItem($key, $value);
95
            }
96
        } else {
97
            $content = $data;
98
        }
99
        return static::tag($tag,$content);
100
    }
101
102
    protected static function tag($name, $content = '', $options = [])
103
    {
104
        return "<$name" . Html::renderTagAttributes($options) . '>' . $content . "</$name>";
105
    }
106
107
}
108