Completed
Push — master ( d073e6...cc81ea )
by Andrii
03:32
created

AbstractTarget::hasId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\target;
12
13
/**
14
 * @see TargetInterface
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
abstract class AbstractTarget implements TargetInterface
19
{
20
    /**
21
     * @var int|string
22
     */
23
    protected $id;
24
25
    /**
26
     * @var string
27
     */
28
    protected $type;
29
30
    /**
31
     * @var string
32
     */
33
    protected $name;
34
35 10
    public function __construct($id, $type, $name = null)
36
    {
37 10
        $this->id = $id;
38 10
        $this->type = $type;
39 10
        $this->name = $name;
40 10
    }
41
42
    /**
43
     * @return int
44
     */
45 9
    public function getId()
46
    {
47 9
        return $this->id;
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function hasId()
54
    {
55
        return $this->id !== null;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 9
    public function getType()
62
    {
63 9
        return $this->type;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getName()
70
    {
71
        return $this->name;
72
    }
73
74
    /**
75
     * @return string
76
     */
77 6
    public function getUniqueId()
78
    {
79 6
        $id = $this->getId();
80 6
        $type = $this->getType();
81
82 6
        return ($type === self::ANY ? '' : $type) . ':' . ($id === self::ANY ? '' : $id);
83
    }
84
85
    /**
86
     * @return bool
87
     */
88 2
    public function equals(TargetInterface $other): bool
89
    {
90 2
        return $this->getUniqueId() === $other->getUniqueId();
91
    }
92
93
    /**
94
     * @return bool
95
     */
96 7
    public function matches(TargetInterface $other): bool
97
    {
98 7
        return $this->checkMatches($other) || $other->checkMatches($this);
99
    }
100
101 7
    public function checkMatches(TargetInterface $other): bool
102
    {
103 7
        if ($this->id === self::ANY) {
104 1
            if ($this->type === self::ANY) {
105 1
                return true;
106
            }
107
108 1
            return $this->matchTypes($other);
109
        }
110
111 7
        if ($this->type === self::ANY) {
112
            return $this->matchIds($other);
113
        }
114
115 7
        return $this->matchIds($other) && $this->matchTypes($other);
116
    }
117
118 7
    protected function matchIds(TargetInterface $other)
119
    {
120 7
        return $this->matchStrings($this->id, $other->getId());
121
    }
122
123 7
    protected function matchTypes(TargetInterface $other)
124
    {
125 7
        return $this->matchStrings($this->type, $other->getType());
126
    }
127
128 7 View Code Duplication
    protected function matchStrings($lhs, $rhs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130 7
        if ($lhs === self::NONE || $rhs === self::NONE) {
131 1
            return false;
132
        }
133
134 7
        return (string) $lhs === (string) $rhs;
135
    }
136
137
    public function jsonSerialize()
138
    {
139
        return get_object_vars($this);
140
    }
141
}
142