Counter::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
 * Interface CounterInterface
7
 * @package sokolnikov911\YandexTurboPages
8
 */
9
class Counter implements CounterInterface
10
{
11
    const TYPE_LIVE_INTERNET    = 'LiveInternet';
12
    const TYPE_GOOGLE_ANALYTICS = 'Google';
13
    const TYPE_MEDIASCOPE       = 'Mediascope';
14
    const TYPE_MAIL_RU          = 'MailRu';
15
    const TYPE_RAMBLER          = 'Rambler';
16
    const TYPE_YANDEX           = 'Yandex';
17
    const TYPE_CUSTOM           = 'custom';
18
19
    private $type;
20
    private $id;
21
    private $url;
22
23 63
    public function __construct($type, $id = null, $url = null)
24
    {
25 63
        $this->type = $type;
26 63
        $this->id   = $id;
27 63
        $this->url  = $url;
28
29 63
        if ($type == self::TYPE_CUSTOM && $url === null) {
30 7
            throw new \UnexpectedValueException('Please set url for custom counter');
31
        }
32
33 56
        if ($type != self::TYPE_CUSTOM && $id === null) {
34 7
            throw new \UnexpectedValueException('Please set id for non custom counter');
35
        }
36 49
    }
37
38 14
    public function appendTo(ChannelInterface $channel)
39
    {
40 14
        $channel->addCounter($this);
41 14
        return $this;
42
    }
43
44 21
    public function asXML()
45
    {
46 21
        $idPart = $this->id ? ' id="' . $this->id . '" ' : '';
47 21
        $urlPart = $this->url ? ' url="' . $this->url . '" ' : '';
48
49 21
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><yandex:analytics type="'
50 21
            . $this->type . '"' . $idPart . $urlPart . ' xmlns:yandex="http://news.yandex.ru"></yandex:analytics>',
51 21
            LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
52
53 21
        return $xml;
54
    }
55
}