Completed
Push — master ( 7b15fd...5c92ba )
by Petro
04:28
created

Item::turboSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

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 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace sokolnikov911\YandexTurboPages;
4
5
/**
6
 * Class Item
7
 * @package sokolnikov911\YandexTurboPages
8
 */
9
class Item implements ItemInterface
10
{
11
    /** @var boolean */
12
    protected $turbo;
13
14
    /** @var string */
15
    protected $title;
16
17
    /** @var string */
18
    protected $link;
19
20
    /** @var string */
21
    protected $category;
22
23
    /** @var int */
24
    protected $pubDate;
25
26
    /** @var string */
27
    protected $author;
28
29
    /** @var string */
30
    protected $fullText;
31
32
    /** @var string */
33
    protected $turboSource;
34
35
    /** @var string */
36
    protected $turboTopic;
37
38
    /** @var string */
39
    protected $turboContent;
40
41
    /** @var RelatedItemsListInterface */
42
    protected $relatedItemsList;
43
44 120
    public function __construct($turbo = true)
45
    {
46 120
        $this->turbo = $turbo;
47 120
    }
48
49 36
    public function title($title)
50
    {
51 36
        $title = (mb_strlen($title) > 240) ? mb_substr($title, 0, 239) . '…' : $title;
52 36
        $this->title = $title;
53 36
        return $this;
54
    }
55
56 30
    public function link($link)
57
    {
58 30
        $this->link = $link;
59 30
        return $this;
60
    }
61
62 30
    public function category($category)
63
    {
64 30
        $this->category = $category;
65 30
        return $this;
66
    }
67
68 36
    public function pubDate($pubDate)
69
    {
70 36
        $this->pubDate = $pubDate;
71 36
        return $this;
72
    }
73
74 12
    public function turboSource($turboSource)
75
    {
76 12
        $this->turboSource = $turboSource;
77 12
        return $this;
78
    }
79
80 12
    public function turboTopic($turboTopic)
81
    {
82 12
        $this->turboTopic = $turboTopic;
83 12
        return $this;
84
    }
85
86 36
    public function turboContent($turboContent)
87
    {
88 36
        $this->turboContent = $turboContent;
89 36
        return $this;
90
    }
91
92 30
    public function author($author)
93
    {
94 30
        $this->author = $author;
95 30
        return $this;
96
    }
97
98 12
    public function fullText($fullText)
99
    {
100 12
        $this->fullText = $fullText;
101 12
        return $this;
102
    }
103
104 6
    public function appendTo(ChannelInterface $channel)
105
    {
106 6
        $channel->addItem($this);
107 6
        return $this;
108
    }
109
110 18
    public function addRelatedItemsList(RelatedItemsListInterface $relatedItemsList)
111
    {
112 18
        $this->relatedItemsList = $relatedItemsList;
113 18
        return $this;
114
    }
115
116 30
    public function asXML()
117
    {
118 30
        $isTurboEnabled = $this->turbo ? 'true' : 'false';
119
120 30
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><item turbo="' . $isTurboEnabled
121 30
            . '"></item>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
122
123 30
        $xml->addChild('title', $this->title);
124 30
        $xml->addChild('link', $this->link);
125 30
        $xml->addCdataChild('turbo:content', $this->turboContent, 'http://turbo.yandex.ru');
126 30
        $xml->addChild('pubDate', date(DATE_RSS, $this->pubDate));
127
128 30
        $xml->addChildWithValueChecking('category', $this->category);
129 30
        $xml->addChildWithValueChecking('author', $this->author);
130 30
        $xml->addChildWithValueChecking('yandex:full-text', $this->fullText, 'http://news.yandex.ru');
131 30
        $xml->addChildWithValueChecking('turbo:topic', $this->turboTopic, 'http://turbo.yandex.ru');
132 30
        $xml->addChildWithValueChecking('turbo:source', $this->turboSource, 'http://turbo.yandex.ru');
133
134 30
        if ($this->relatedItemsList) {
135 6
            $toDom = dom_import_simplexml($xml);
136 6
            $fromDom = dom_import_simplexml($this->relatedItemsList->asXML());
137 6
            $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
138 3
        }
139
140 30
        return $xml;
141
    }
142
}
143