Completed
Push — master ( 291758...cb1c4d )
by Dmitry
02:39
created

AbstractTarget::getName()   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
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
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
    public function __construct($id, $type, $name)
36
    {
37
        $this->id = $id;
38
        $this->type = $type;
39
        $this->name = $name;
40
    }
41
42
    /**
43
     * @return int
44
     */
45
    public function getId()
46
    {
47
        return $this->id;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getType()
54
    {
55
        return $this->type;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getName()
62
    {
63
        return $this->name;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getUniqueId()
70
    {
71
        $id = $this->getId();
72
        $type = $this->getType();
73
74
        return ($type === self::ANY ? '' : $type) . ':' . ($id === self::ANY ? '' : $id);
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function equals(TargetInterface $other): bool
81
    {
82
        return $this->getUniqueId() === $other->getUniqueId();
83
    }
84
85
    /**
86
     * @return bool
87
     */
88
    public function matches(TargetInterface $other): bool
89
    {
90
        return $this->checkMatches($other) || $other->checkMatches($this);
91
    }
92
93
    public function checkMatches(TargetInterface $other): bool
94
    {
95
        if ($this->id === self::ANY) {
96
            if ($this->type === self::ANY) {
97
                return true;
98
            }
99
100
            return $this->matchTypes($other);
101
        }
102
103
        if ($this->type === self::ANY) {
104
            return $this->matchIds($other);
105
        }
106
107
        return $this->matchIds($other) && $this->matchTypes($other);
108
    }
109
110
    protected function matchIds(TargetInterface $other)
111
    {
112
        return $this->matchStrings($this->id, $other->getId());
113
    }
114
115
    protected function matchTypes(TargetInterface $other)
116
    {
117
        return $this->matchStrings($this->type, $other->getType());
118
    }
119
120 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...
121
    {
122
        if ($lhs === self::NONE || $rhs === self::NONE) {
123
            return false;
124
        }
125
126
        return (string) $lhs === (string) $rhs;
127
    }
128
129
    public function jsonSerialize()
130
    {
131
        return get_object_vars($this);
132
    }
133
}
134