Channel   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 4
dl 0
loc 134
ccs 62
cts 62
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A title() 0 6 2
A link() 0 5 1
A description() 0 5 1
A language() 0 5 1
A adNetwork() 0 9 1
A addItem() 0 5 1
A addCounter() 0 5 1
A appendTo() 0 5 1
C asXML() 0 45 12
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 42
    public function title($title)
45
    {
46 42
        $title = (mb_strlen($title) > 240) ? mb_substr($title, 0, 239) . '…' : $title;
47 42
        $this->title = $title;
48 42
        return $this;
49
    }
50
51 28
    public function link($link)
52
    {
53 28
        $this->link = $link;
54 28
        return $this;
55
    }
56
57 21
    public function description($description)
58
    {
59 21
        $this->description = $description;
60 21
        return $this;
61
    }
62
63 14
    public function language($language)
64
    {
65 14
        $this->language = $language;
66 14
        return $this;
67
    }
68
69 21
    public function adNetwork($type = self::AD_TYPE_YANDEX, $id = '', $turboAdId, $code = '')
70
    {
71 21
        $this->adType      = $type;
72 21
        $this->adId        = $id;
73 21
        $this->adTurboAdId = $turboAdId;
74 21
        $this->adCode      = $code;
75
76 21
        return $this;
77
    }
78
79 21
    public function addItem(ItemInterface $item)
80
    {
81 21
        $this->items[] = $item;
82 21
        return $this;
83
    }
84
85 21
    public function addCounter(CounterInterface $counter)
86
    {
87 21
        $this->counters[] = $counter;
88 21
        return $this;
89
    }
90
91 14
    public function appendTo(FeedInterface $feed)
92
    {
93 14
        $feed->addChannel($this);
94 14
        return $this;
95
    }
96
97 56
    public function asXML()
98
    {
99 56
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><channel></channel>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
100 56
        $xml->addChild('title', $this->title);
101 56
        $xml->addChild('link', $this->link);
102 56
        $xml->addChild('description', $this->description);
103
104 56
        $xml->addChildWithValueChecking('language', $this->language);
105
106 56
        if ($this->adType &&
107 14
            ((($this->adType == Channel::AD_TYPE_YANDEX) && $this->adId) ||
108 56
                (($this->adType == Channel::AD_TYPE_ADFOX) && $this->adCode))) {
109
110 14
            $adChild = $xml->addChild('yandex:adNetwork', '', 'http://news.yandex.ru');
111 14
            $adChild->addAttribute('type', $this->adType);
112
113 14
            if ($this->adId) {
114 7
                $adChild->addAttribute('id', $this->adId);
115 3
            }
116
117 14
            if ($this->adTurboAdId) {
118 14
                $adChild->addAttribute('turbo-ad-id', $this->adTurboAdId);
119 6
            }
120
121 14
            if (($this->adType == self::AD_TYPE_ADFOX) && $this->adCode) {
122 7
                $dom = dom_import_simplexml($adChild);
123 7
                $elementOwner = $dom->ownerDocument;
124 7
                $dom->appendChild($elementOwner->createCDATASection($this->adCode));
125 3
            }
126 6
        }
127
128 56
        foreach ($this->counters as $counter) {
129 7
            $toDom = dom_import_simplexml($xml);
130 7
            $fromDom = dom_import_simplexml($counter->asXML());
131 7
            $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
132 24
        }
133
134 56
        foreach ($this->items as $item) {
135 7
            $toDom = dom_import_simplexml($xml);
136 7
            $fromDom = dom_import_simplexml($item->asXML());
137 7
            $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
138 24
        }
139
140 56
        return $xml;
141
    }
142
}
143