Failed Conditions
Push — issue#702_rs ( ed72a1...cdafcf )
by Guilherme
07:33
created

VersionService::getLatestVersion()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 16
nop 3
dl 0
loc 22
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
    public function __construct(RequestStack $requestStack, array $supportedVersions)
30
    {
31
        $this->requestStack = $requestStack;
32
        $this->supportedVersions = $supportedVersions;
33
    }
34
35
    /**
36
     * @param int|null $major
37
     * @param int|null $minor
38
     * @param int|null $patch
39
     * @return array
40
     */
41
    public function getLatestVersion($major = null, $minor = null, $patch = null)
42
    {
43
        if ($major === null) {
44
            $major = max(array_keys($this->supportedVersions));
45
        }
46
        if ($minor === null) {
47
            $minor = max(array_keys($this->supportedVersions[$major]));
48
        }
49
        if ($patch === null) {
50
            $patch = max($this->supportedVersions[$major][$minor]);
51
        }
52
53
        if (false === array_key_exists($major, $this->supportedVersions)
54
            || false === array_key_exists($minor, $this->supportedVersions[$major])
55
            || false === array_search($patch, $this->supportedVersions[$major][$minor])) {
56
            throw new \InvalidArgumentException("Invalid API version");
57
        }
58
59
        return [
60
            'major' => $major,
61
            'minor' => $minor,
62
            'patch' => $patch,
63
        ];
64
    }
65
66
    /**
67
     * @param Request $request
68
     * @return array
69
     */
70
    public function getVersionFromRequest(Request $request = null)
71
    {
72
        if ($request === null) {
73
            $request = $this->requestStack->getCurrentRequest();
74
        }
75
76
        $pathVersion = $request->attributes->get('version');
77
        $version = explode('.', $pathVersion, 3);
78
79
        $major = $minor = $patch = null;
80
        if (array_key_exists(0, $version)) {
81
            $major = $version[0];
82
        }
83
        if (array_key_exists(1, $version)) {
84
            $minor = $version[1];
85
        }
86
        if (array_key_exists(2, $version)) {
87
            $patch = $version[2];
88
        }
89
90
        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
    public function getString(array $components)
98
    {
99
        return implode('.', $components);
100
    }
101
}
102