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

RelatedItemsList   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
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 46
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 16 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
    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