Channel::link()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace sokolnikov911\YandexTurboPages;
4
5
/**
6
 * Class Channel
7
 * @package sokolnikov911\YandexTurboPages
8
 */
9
class Channel implements ChannelInterface
10
{
11
    const AD_TYPE_YANDEX = 'Yandex';
12
    const AD_TYPE_ADFOX = 'AdFox';
13
14
    /** @var string */
15
    protected $title;
16
17
    /** @var string */
18
    protected $link;
19
20
    /** @var string */
21
    protected $description;
22
23
    /** @var string */
24
    protected $language;
25
26
    /** @var string */
27
    protected $adType;
28
29
    /** @var string */
30
    protected $adId;
31
32
    /** @var string */
33
    protected $adTurboAdId;
34
35
    /** @var string */
36
    protected $adCode;
37
38
    /** @var ItemInterface[] */
39
    protected $items = [];
40
41
    /** @var CounterInterface[] */
42
    protected $counters = [];
43
44 24
    public function title(string $title): ChannelInterface
45
    {
46 24
        $title = (mb_strlen($title) > 240) ? mb_substr($title, 0, 239) . '…' : $title;
47 24
        $this->title = $title;
48 24
        return $this;
49
    }
50
51 16
    public function link(string $link): ChannelInterface
52
    {
53 16
        $this->link = $link;
54 16
        return $this;
55
    }
56
57 12
    public function description(string $description): ChannelInterface
58
    {
59 12
        $this->description = $description;
60 12
        return $this;
61
    }
62
63 8
    public function language(string $language): ChannelInterface
64
    {
65 8
        $this->language = $language;
66 8
        return $this;
67
    }
68
69 20
    public function adNetwork(string $type = self::AD_TYPE_YANDEX, string $id = '',
70
                              string $turboAdId, string $code = ''): ChannelInterface
71
    {
72 20
        $this->adType      = $type;
73 20
        $this->adId        = $id;
74 20
        $this->adTurboAdId = $turboAdId;
75 20
        $this->adCode      = $code;
76
77 20
        if ($type == self::AD_TYPE_YANDEX && !$id) {
78 4
            throw new \UnexpectedValueException('Please set id for Yandex Ad');
79
        }
80
81 16
        if ($type == self::AD_TYPE_ADFOX && !$this->adCode) {
82 4
            throw new \UnexpectedValueException('Please set code for Adfox network');
83
        }
84
85 12
        return $this;
86
    }
87
88 12
    public function addItem(ItemInterface $item): ChannelInterface
89
    {
90 12
        $this->items[] = $item;
91 12
        return $this;
92
    }
93
94 12
    public function addCounter(CounterInterface $counter): ChannelInterface
95
    {
96 12
        $this->counters[] = $counter;
97 12
        return $this;
98
    }
99
100 8
    public function appendTo(FeedInterface $feed): ChannelInterface
101
    {
102 8
        $feed->addChannel($this);
103 8
        return $this;
104
    }
105
106 32
    public function asXML(): SimpleXMLElement
107
    {
108 32
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><channel></channel>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
109 32
        $xml->addChild('title', $this->title);
110 32
        $xml->addChild('link', $this->link);
111 32
        $xml->addChild('description', $this->description);
112
113 32
        $xml->addChildWithValueChecking('language', $this->language);
114
115 32
        if ($this->adType) {
116
117 8
            $adChild = $xml->addChild('yandex:adNetwork', '', 'http://news.yandex.ru');
118 8
            $adChild->addAttribute('type', $this->adType);
119
120 8
            if ($this->adId) {
121 4
                $adChild->addAttribute('id', $this->adId);
122
            }
123
124 8
            if ($this->adTurboAdId) {
125 8
                $adChild->addAttribute('turbo-ad-id', $this->adTurboAdId);
126
            }
127
128 8
            if ($this->adType == self::AD_TYPE_ADFOX) {
129 4
                $dom = dom_import_simplexml($adChild);
130 4
                $elementOwner = $dom->ownerDocument;
131 4
                $dom->appendChild($elementOwner->createCDATASection($this->adCode));
132
            }
133
        }
134
135 32
        foreach ($this->counters as $counter) {
136 4
            $toDom = dom_import_simplexml($xml);
137 4
            $fromDom = dom_import_simplexml($counter->asXML());
138 4
            $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
139
        }
140
141 32
        foreach ($this->items as $item) {
142 4
            $toDom = dom_import_simplexml($xml);
143 4
            $fromDom = dom_import_simplexml($item->asXML());
144 4
            $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
145
        }
146
147 32
        return $xml;
148
    }
149
}
150