Completed
Push — master ( 791b1e...fde4e1 )
by Andrii
02:28
created

AbstractTarget::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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 10
    public function __construct($id, $type)
31
    {
32 10
        $this->id = $id;
33 10
        $this->type = $type;
34 10
    }
35
36
    /**
37
     * @return int
38
     */
39 8
    public function getId()
40
    {
41 8
        return $this->id;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 8
    public function getType()
48
    {
49 8
        return $this->type;
50
    }
51
52
    /**
53
     * @return string
54
     */
55 5
    public function getUniqueId()
56
    {
57 5
        $id = $this->getId();
58 5
        $type = $this->getType();
59
60 5
        return ($type === self::ANY ? '' : $type) . ':' . ($id === self::ANY ? '' : $id);
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $type (string) and self::ANY (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
61
    }
62
63
    /**
64
     * @return bool
65
     */
66 2
    public function equals(TargetInterface $other): bool
67
    {
68 2
        return $this->getUniqueId() === $other->getUniqueId();
69
    }
70
71
    /**
72
     * @return bool
73
     */
74 6
    public function matches(TargetInterface $other): bool
75
    {
76 6
        return $this->checkMatches($other) || $other->checkMatches($this);
77
    }
78
79 6
    public function checkMatches(TargetInterface $other): bool
80
    {
81 6
        if ($this->id === self::ANY) {
82 1
            if ($this->type === self::ANY) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $this->type (string) and self::ANY (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
83 1
                return true;
84
            }
85
86 1
            return $this->matchTypes($other);
87
        }
88
89 6
        if ($this->type === self::ANY) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $this->type (string) and self::ANY (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
90
            return $this->matchIds($other);
91
        }
92
93 6
        return $this->matchIds($other) && $this->matchTypes($other);
94
    }
95
96 6
    protected function matchIds(TargetInterface $other)
97
    {
98 6
        return $this->matchStrings($this->id, $other->getId());
99
    }
100
101 6
    protected function matchTypes(TargetInterface $other)
102
    {
103 6
        return $this->matchStrings($this->type, $other->getType());
104
    }
105
106 6 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...
107
    {
108 6
        if ($lhs === self::NONE || $rhs === self::NONE) {
109 1
            return false;
110
        }
111
112 6
        return (string) $lhs === (string) $rhs;
113
    }
114
115
116
    public function jsonSerialize()
117
    {
118
        return get_object_vars($this);
119
    }
120
}
121