RelatedItemsList::appendTo()   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 RelatedItemsList
7
 * @package sokolnikov911\YandexTurboPages
8
 */
9
class RelatedItemsList implements RelatedItemsListInterface
10
{
11
    /** @var RelatedItemInterface[] */
12
    protected $relatedItems = [];
13
14
    /** @var bool */
15
    protected $infinity;
16
17
    /**
18
     * Add channel
19
     * @param bool $infinity Use or not infinity scroll of related items
20
     */
21 49
    public function __construct($infinity = false)
22
    {
23 49
        $this->infinity = $infinity;
24 49
    }
25
26 7
    public function appendTo(ItemInterface $item)
27
    {
28 7
        $item->addRelatedItemsList($this);
29 7
        return $this;
30
    }
31
32 21
    public function addItem(RelatedItem $relatedItem)
33
    {
34 21
        $this->relatedItems[] = $relatedItem;
35 21
        return $this;
36
    }
37
38 28
    public function asXML()
39
    {
40 28
        $infinity = $this->infinity ? 'type="infinity"' : '';
41
42 28
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><yandex:related xmlns:yandex="http://news.yandex.ru" '
43 28
            . $infinity . '></yandex:related>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
44
45 28
        foreach ($this->relatedItems as $item) {
46 7
            $toDom = dom_import_simplexml($xml);
47 7
            $fromDom = dom_import_simplexml($item->asXML());
48 7
            $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
49 12
        }
50
51 28
        return $xml;
52
    }
53
}
54