Definition   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 39
c 2
b 1
f 0
dl 0
loc 136
ccs 44
cts 44
cp 1
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isUntilVersionAtLeastSameAsSince() 0 11 4
A jsonSerialize() 0 6 1
A getUntil() 0 3 1
A create() 0 24 1
A __construct() 0 5 1
A isAvailableForVersion() 0 6 4
A __toString() 0 3 1
A getSince() 0 3 1
A getName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiechu\SymfonyCommonsBundle\ApiFeature;
6
7
use Spiechu\SymfonyCommonsBundle\Utils\AssertUtils;
8
9
class Definition implements \JsonSerializable
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $name;
15
16
    /**
17
     * @var null|string
18
     */
19
    protected $since;
20
21
    /**
22
     * @var null|string
23
     */
24
    protected $until;
25
26
    /**
27
     * @param string      $name
28
     * @param null|string $since
29
     * @param null|string $until
30
     */
31 4
    protected function __construct(string $name, ?string $since, ?string $until)
32
    {
33 4
        $this->name = $name;
34 4
        $this->since = $since;
35 4
        $this->until = $until;
36 4
    }
37
38
    /**
39
     * @return string
40
     */
41 1
    public function __toString(): string
42
    {
43 1
        return $this->name;
44
    }
45
46
    /**
47
     * @param string      $name
48
     * @param null|string $since
49
     * @param null|string $until
50
     *
51
     * @return static
52
     */
53 9
    public static function create(string $name, ?string $since, ?string $until)
54
    {
55 9
        \assert(
56 9
            AssertUtils::isNotEmpty($name),
57 9
            'Empty feature name'
58
        );
59 8
        \assert(
60 8
            AssertUtils::isNumericOrNull($since),
61 8
            'Since parameter is not numeric'
62
        );
63 7
        \assert(
64 7
            AssertUtils::isNumericOrNull($until),
65 7
            'Until parameter is not numeric'
66
        );
67 6
        \assert(
68 6
            AssertUtils::isAtLeastOneArgumentNotNull($since, $until),
69 6
            'No version constraints provided'
70
        );
71 5
        \assert(
72 5
            static::isUntilVersionAtLeastSameAsSince($since, $until),
73 5
            'Until parameter is lower than since parameter'
74
        );
75
76 4
        return new static($name, $since, $until);
77
    }
78
79
    /**
80
     * @return string
81
     */
82 4
    public function getName(): string
83
    {
84 4
        return $this->name;
85
    }
86
87
    /**
88
     * @return null|string
89
     */
90 1
    public function getSince(): ?string
91
    {
92 1
        return $this->since;
93
    }
94
95
    /**
96
     * @return null|string
97
     */
98 1
    public function getUntil(): ?string
99
    {
100 1
        return $this->until;
101
    }
102
103
    /**
104
     * @param string $version
105
     *
106
     * @return bool
107
     */
108 3
    public function isAvailableForVersion(string $version): bool
109
    {
110 3
        $sinceVersionMatch = null === $this->since || version_compare($version, $this->since, '>=');
111 3
        $untilVersionMatch = null === $this->until || version_compare($version, $this->until, '<=');
112
113 3
        return $sinceVersionMatch && $untilVersionMatch;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 2
    public function jsonSerialize(): array
120
    {
121
        return [
122 2
            'name' => $this->name,
123 2
            'since' => $this->since,
124 2
            'until' => $this->until,
125
        ];
126
    }
127
128
    /**
129
     * @param null|string $since
130
     * @param null|string $until
131
     *
132
     * @return bool
133
     */
134 5
    protected static function isUntilVersionAtLeastSameAsSince(?string $since, ?string $until): bool
135
    {
136 5
        if (null === $since || null === $until) {
137 3
            return true;
138
        }
139
140 5
        if (version_compare($until, $since, '<')) {
141 1
            return false;
142
        }
143
144 4
        return true;
145
    }
146
}
147