Completed
Pull Request — master (#396)
by Luc
04:25
created

SapiVersion   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 61
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A toNative() 0 4 1
A equals() 0 4 1
A allowedValues() 0 7 1
A guardSapiVersion() 0 8 2
1
<?php
2
3
namespace CultuurNet\UDB3\ValueObject;
4
5
class SapiVersion
6
{
7
    const V2 = 'v2';
8
    const V3 = 'v3';
9
10
    /**
11
     * @var string
12
     */
13
    private $version;
14
15
    /**
16
     * @param string $version
17
     */
18
    public function __construct(string $version)
19
    {
20
        $this->guardSapiVersion($version);
21
22
        $this->version = $version;
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function toNative(): string
29
    {
30
        return $this->version;
31
    }
32
33
    /**
34
     * @param SapiVersion $sapiVersion
35
     * @return bool
36
     */
37
    public function equals(SapiVersion $sapiVersion): bool
38
    {
39
        return $this->version === $sapiVersion->toNative();
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    private function allowedValues(): array
46
    {
47
        return [
48
            self::V2,
49
            self::V3,
50
        ];
51
    }
52
53
    /**
54
     * @param string $version
55
     * @throws \InvalidArgumentException
56
     */
57
    private function guardSapiVersion(string $version)
58
    {
59
        if (!in_array($version, $this->allowedValues())) {
60
            throw new \InvalidArgumentException(
61
                'Sapi version "' . $version . '" is not allowed.'
62
            );
63
        }
64
    }
65
}
66