Completed
Push — master ( 7b15fd...5c92ba )
by Petro
04:28
created

Counter::__construct()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.2742

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 9
cp 0.7778
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 3
nop 3
crap 5.2742
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 42
    public function __construct($type, $id = null, $url = null)
24
    {
25 42
        $this->type = $type;
26 42
        $this->id   = $id;
27 42
        $this->url  = $url;
28
29 42
        if ($type == self::TYPE_CUSTOM && !$url) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $url of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
30
            throw new \UnexpectedValueException('Please set url for custom counter');
31
        }
32
33 42
        if ($type != self::TYPE_CUSTOM && !$id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
34
            throw new \UnexpectedValueException('Please set id for non custom counter');
35
        }
36 42
    }
37
38 12
    public function appendTo(ChannelInterface $channel)
39
    {
40 12
        $channel->addCounter($this);
41 12
        return $this;
42
    }
43
44 18
    public function asXML()
45
    {
46 18
        $idPart = $this->id ? ' id="' . $this->id . '" ' : '';
47 18
        $urlPart = $this->url ? ' url="' . $this->url . '" ' : '';
48
49 18
        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><yandex:analytics type="'
50 18
            . $this->type . '"' . $idPart . $urlPart . ' xmlns:yandex="http://news.yandex.ru"></yandex:analytics>',
51 18
            LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
52
53 18
        return $xml;
54
    }
55
}