Completed
Push — master ( b44d6e...953f71 )
by Petro
03:12
created

RelatedItemsList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
    protected $infinity;
15
16
    /**
17
     * Add channel
18
     * @param bool $infinity Use or not infinity scroll of related items
19
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
20
     */
21 21
    public function __construct(bool $infinity = false)
22
    {
23 21
        $this->infinity = $infinity;
24 21
    }
25
26 3
    public function appendTo(ItemInterface $item): RelatedItemsListInterface
27
    {
28 3
        $item->addRelatedItemsList($this);
29 3
        return $this;
30
    }
31
32 9
    public function addItem(RelatedItem $relatedItem): RelatedItemsListInterface
33
    {
34 9
        $this->relatedItems[] = $relatedItem;
35 9
        return $this;
36
    }
37
38 12
    public function asXML(): SimpleXMLElement
39
    {
40 12
        $infinity = $this->infinity ? 'type="infinity"' : '';
41
42 12
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><yandex:related '
43 12
            . $infinity . '></yandex:related>',
44 12
            LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
45
46 12
        foreach ($this->relatedItems as $item) {
47 3
            $toDom = dom_import_simplexml($xml);
48 3
            $fromDom = dom_import_simplexml($item->asXML());
49 3
            $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
50
        }
51
52 12
        return $xml;
53
    }
54
}
55