Completed
Push — master ( e45d47...fbc4e2 )
by Dawid
03:25
created

Definition::create()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 3
dl 0
loc 24
ccs 17
cts 17
cp 1
crap 1
rs 8.9713
c 0
b 0
f 0
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
     * @throws \InvalidArgumentException
52
     *
53
     * @return static
54
     */
55 9
    public static function create(string $name, ?string $since, ?string $until)
56
    {
57 9
        \assert(
58 9
            AssertUtils::isNotEmpty($name),
59 9
            'Empty feature name'
60
        );
61 8
        \assert(
62 8
            AssertUtils::isNumericOrNull($since),
63 8
            'Since parameter is not numeric'
64
        );
65 7
        \assert(
66 7
            AssertUtils::isNumericOrNull($until),
67 7
            'Until parameter is not numeric'
68
        );
69 6
        \assert(
70 6
            AssertUtils::isAtLeastOneArgumentNotNull($since, $until),
71 6
            'No version constraints provided'
72
        );
73 5
        \assert(
74 5
            static::isUntilVersionAtLeastSameAsSince($since, $until),
75 5
            'Until parameter is lower than since parameter'
76
        );
77
78 4
        return new static($name, $since, $until);
79
    }
80
81
    /**
82
     * @return string
83
     */
84 4
    public function getName(): string
85
    {
86 4
        return $this->name;
87
    }
88
89
    /**
90
     * @return null|string
91
     */
92 1
    public function getSince(): ?string
93
    {
94 1
        return $this->since;
95
    }
96
97
    /**
98
     * @return null|string
99
     */
100 1
    public function getUntil(): ?string
101
    {
102 1
        return $this->until;
103
    }
104
105
    /**
106
     * @param string $version
107
     *
108
     * @return bool
109
     */
110 3
    public function isAvailableForVersion(string $version): bool
111
    {
112 3
        $sinceVersionMatch = null === $this->since || version_compare($version, $this->since, '>=');
113 3
        $untilVersionMatch = null === $this->until || version_compare($version, $this->until, '<=');
114
115 3
        return $sinceVersionMatch && $untilVersionMatch;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 2
    public function jsonSerialize(): array
122
    {
123
        return [
124 2
            'name' => $this->name,
125 2
            'since' => $this->since,
126 2
            'until' => $this->until,
127
        ];
128
    }
129
130
    /**
131
     * @param null|string $since
132
     * @param null|string $until
133
     *
134
     * @return bool
135
     */
136 5
    protected static function isUntilVersionAtLeastSameAsSince(?string $since, ?string $until): bool
137
    {
138 5
        if (null === $since || null === $until) {
139 3
            return true;
140
        }
141
142 5
        if (version_compare($until, $since, '<')) {
143 1
            return false;
144
        }
145
146 4
        return true;
147
    }
148
}
149