Completed
Push — master ( 78bde8...37bfe4 )
by Petro
06:02
created

RelatedItemsList   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 45
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A appendTo() 0 5 1
A addItem() 0 5 1
A asXML() 0 15 3
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 42
    public function __construct($infinity = false)
22
    {
23 42
        $this->infinity = $infinity;
24 42
    }
25
26 6
    public function appendTo(ItemInterface $item)
27
    {
28 6
        $item->addRelatedItemsList($this);
29 6
        return $this;
30
    }
31
32 18
    public function addItem(RelatedItem $relatedItem)
33
    {
34 18
        $this->relatedItems[] = $relatedItem;
35 18
        return $this;
36
    }
37
38 24
    public function asXML()
39
    {
40 24
        $infinity = $this->infinity ? 'type="infinity"' : '';
41
42 24
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><yandex:related xmlns:yandex="http://news.yandex.ru" '
43 24
            . $infinity . '></yandex:related>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
44
45 24
        foreach ($this->relatedItems as $item) {
46 6
            $toDom = dom_import_simplexml($xml);
47 6
            $fromDom = dom_import_simplexml($item->asXML());
48 6
            $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
49 12
        }
50
51 24
        return $xml;
52
    }
53
}
54