Completed
Push — master ( 741118...651715 )
by Dawid
03:43
created

Definition::__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
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
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\StringUtils;
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 Definition
54
     */
55 9
    public static function create(string $name, ?string $since, ?string $until): Definition
56
    {
57 9
        StringUtils::assertNotEmpty($name, 'Empty feature name');
58 8
        StringUtils::assertNumericOrNull($since, 'Since parameter is not numeric');
59 7
        StringUtils::assertNumericOrNull($until, 'Until parameter is not numeric');
60 6
        StringUtils::assertAtLeastOneArgumentNotNull('No version constraints provided', $since, $until);
61
62 5
        static::assertUntilVersionAtLeastSameAsSince($since, $until);
63
64 4
        return new static($name, $since, $until);
65
    }
66
67
    /**
68
     * @return string
69
     */
70 4
    public function getName(): string
71
    {
72 4
        return $this->name;
73
    }
74
75
    /**
76
     * @return null|string
77
     */
78 1
    public function getSince(): ?string
79
    {
80 1
        return $this->since;
81
    }
82
83
    /**
84
     * @return null|string
85
     */
86 1
    public function getUntil(): ?string
87
    {
88 1
        return $this->until;
89
    }
90
91
    /**
92
     * @param string $version
93
     *
94
     * @return bool
95
     */
96 3
    public function isAvailableForVersion(string $version): bool
97
    {
98 3
        $sinceVersionMatch = null === $this->since || version_compare($version, $this->since, '>=');
99 3
        $untilVersionMatch = null === $this->until || version_compare($version, $this->until, '<=');
100
101 3
        return $sinceVersionMatch && $untilVersionMatch;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 2
    public function jsonSerialize(): array
108
    {
109
        return [
110 2
            'name' => $this->name,
111 2
            'since' => $this->since,
112 2
            'until' => $this->until,
113
        ];
114
    }
115
116
    /**
117
     * @param null|string $since
118
     * @param null|string $until
119
     *
120
     * @throws \InvalidArgumentException When $until parameter is lower than $since parameter
121
     */
122 5
    protected static function assertUntilVersionAtLeastSameAsSince(?string $since, ?string $until): void
123
    {
124 5
        if ($since === null || $until === null) {
125 3
            return;
126
        }
127
128 5
        if (version_compare($until, $since, '<')) {
129 1
            throw new \InvalidArgumentException('Until parameter is lower than since parameter');
130
        }
131 4
    }
132
}
133