Counter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 47
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 5
A appendTo() 0 5 1
A asXML() 0 11 3
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 36
    public function __construct(string $type, string $id = null, string $url = null)
24
    {
25 36
        $this->type = $type;
26 36
        $this->id   = $id;
27 36
        $this->url  = $url;
28
29 36
        if ($type == self::TYPE_CUSTOM && !isset($url)) {
30 4
            throw new \UnexpectedValueException('Please set url for custom counter');
31
        }
32
33 32
        if ($type != self::TYPE_CUSTOM && !isset($id)) {
34 4
            throw new \UnexpectedValueException('Please set id for non custom counter');
35
        }
36 28
    }
37
38 8
    public function appendTo(ChannelInterface $channel): CounterInterface
39
    {
40 8
        $channel->addCounter($this);
41 8
        return $this;
42
    }
43
44 12
    public function asXML(): SimpleXMLElement
45
    {
46 12
        $idPart = $this->id ? ' id="' . $this->id . '" ' : '';
47 12
        $urlPart = $this->url ? ' url="' . $this->url . '" ' : '';
48
49 12
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><yandex:analytics type="'
50 12
            . $this->type . '"' . $idPart . $urlPart . '></yandex:analytics>',
51 12
            LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
52
53 12
        return $xml;
54
    }
55
}