Passed
Pull Request — master (#790)
by Guilherme
06:37 queued 02:01
created

VersionService::getLatestVersion()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 16
nop 3
dl 0
loc 22
ccs 14
cts 14
cp 1
crap 7
rs 6.9811
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\APIBundle\Service;
12
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\RequestStack;
15
16
class VersionService
17
{
18
    /** @var RequestStack */
19
    private $requestStack;
20
21
    /** @var array */
22
    private $supportedVersions;
23
24
    /**
25
     * VersionService constructor.
26
     * @param RequestStack $requestStack
27
     * @param array $supportedVersions
28
     */
29 6
    public function __construct(RequestStack $requestStack, array $supportedVersions)
30
    {
31 6
        $this->requestStack = $requestStack;
32 6
        $this->supportedVersions = $supportedVersions;
33 6
    }
34
35
    /**
36
     * @param int|null $major
37
     * @param int|null $minor
38
     * @param int|null $patch
39
     * @return array
40
     */
41 5
    public function getLatestVersion($major = null, $minor = null, $patch = null)
42
    {
43 5
        if ($major === null) {
44 1
            $major = max(array_keys($this->supportedVersions));
45
        }
46 5
        if ($minor === null) {
47 5
            $minor = max(array_keys($this->supportedVersions[$major]));
48
        }
49 5
        if ($patch === null) {
50 5
            $patch = max($this->supportedVersions[$major][$minor]);
51
        }
52
53 5
        if (false === array_key_exists($major, $this->supportedVersions)
54 3
            || false === array_key_exists($minor, $this->supportedVersions[$major])
55 5
            || false === array_search($patch, $this->supportedVersions[$major][$minor])) {
56 2
            throw new \InvalidArgumentException("Invalid API version");
57
        }
58
59
        return [
60 3
            'major' => $major,
61 3
            'minor' => $minor,
62 3
            'patch' => $patch,
63
        ];
64
    }
65
66
    /**
67
     * @param Request $request
68
     * @return array
69
     */
70 1
    public function getVersionFromRequest(Request $request = null)
71
    {
72 1
        if ($request === null) {
73 1
            $request = $this->requestStack->getCurrentRequest();
74
        }
75
76 1
        $pathVersion = $request->attributes->get('version');
77 1
        $version = explode('.', $pathVersion, 3);
78
79 1
        $major = $minor = $patch = null;
80 1
        if (array_key_exists(0, $version)) {
81 1
            $major = $version[0];
82
        }
83 1
        if (array_key_exists(1, $version)) {
84 1
            $minor = $version[1];
85
        }
86 1
        if (array_key_exists(2, $version)) {
87 1
            $patch = $version[2];
88
        }
89
90 1
        return $this->getLatestVersion($major, $minor, $patch);
0 ignored issues
show
Bug introduced by
It seems like $patch can also be of type string; however, parameter $patch of LoginCidadao\APIBundle\S...ice::getLatestVersion() does only seem to accept null|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        return $this->getLatestVersion($major, $minor, /** @scrutinizer ignore-type */ $patch);
Loading history...
Bug introduced by
It seems like $minor can also be of type string; however, parameter $minor of LoginCidadao\APIBundle\S...ice::getLatestVersion() does only seem to accept null|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        return $this->getLatestVersion($major, /** @scrutinizer ignore-type */ $minor, $patch);
Loading history...
Bug introduced by
It seems like $major can also be of type string; however, parameter $major of LoginCidadao\APIBundle\S...ice::getLatestVersion() does only seem to accept null|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
        return $this->getLatestVersion(/** @scrutinizer ignore-type */ $major, $minor, $patch);
Loading history...
91
    }
92
93
    /**
94
     * @param array $components
95
     * @return string
96
     */
97 1
    public function getString(array $components)
98
    {
99 1
        return implode('.', $components);
100
    }
101
}
102