RelatedItem::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 RelatedItem
7
 * @package sokolnikov911\YandexTurboPages
8
 */
9
class RelatedItem implements RelatedItemInterface
10
{
11
    /** @var string */
12
    protected $title;
13
14
    /** @var string */
15
    protected $link;
16
17
    /** @var string */
18
    protected $img;
19
20 20
    public function __construct(string $title, string $link, string $img = '')
21
    {
22 20
        $this->link = $link;
23 20
        $this->title = $title;
24 20
        $this->img = $img;
25 20
    }
26
27 4
    public function appendTo(RelatedItemsListInterface $relatedItemsList): RelatedItemInterface
28
    {
29 4
        $relatedItemsList->addItem($this);
30 4
        return $this;
31
    }
32
33 8
    public function asXML(): SimpleXMLElement
34
    {
35 8
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><link>'
36 8
            . $this->title . '</link>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
37
38 8
        $xml->addAttribute('url', $this->link);
39
40 8
        if (!empty($this->img)) {
41 8
            $xml->addAttribute('img', $this->img);
42
        }
43
44 8
        return $xml;
45
    }
46
}
47