SymfonyVersion   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 5
dl 0
loc 109
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 26 5
A getLabel() 0 4 1
A getLatestVersion() 0 35 5
A getResponseAndDecode() 0 21 3
1
<?php
2
3
namespace Liip\MonitorBundle\Check;
4
5
use Laminas\Diagnostics\Check\CheckInterface;
6
use Laminas\Diagnostics\Result\Failure;
7
use Laminas\Diagnostics\Result\Success;
8
use Laminas\Diagnostics\Result\Warning;
9
use Symfony\Component\HttpClient\HttpClient;
10
use Symfony\Component\HttpKernel\Kernel;
11
12
/**
13
 * Checks the version of this app against the latest stable release.
14
 *
15
 * @author Roderik van der Veer <[email protected]>
16
 * @author Kevin Bond <[email protected]>
17
 */
18
class SymfonyVersion implements CheckInterface
19
{
20
    const PACKAGIST_URL = 'https://packagist.org/packages/symfony/symfony.json';
21
    const VERSION_CHECK_URL = 'https://symfony.com/releases/%s.json';
22
23
    public function check()
24
    {
25
        $currentBranch = Kernel::MAJOR_VERSION.'.'.Kernel::MINOR_VERSION;
26
27
        // use symfony.com version checker to see if current branch is still maintained
28
        $response = $this->getResponseAndDecode(sprintf(self::VERSION_CHECK_URL, $currentBranch));
29
30
        if (!isset($response['eol']) || !isset($response['is_eoled'])) {
31
            throw new \Exception('Invalid response from Symfony version checker.');
32
        }
33
34
        $endOfLife = \DateTime::createFromFormat('m/Y', $response['eol'])->format('F, Y');
35
36
        if (true === $response['is_eoled']) {
37
            return new Failure(sprintf('Symfony branch "%s" reached it\'s end of life in %s.', $currentBranch, $endOfLife));
38
        }
39
40
        $currentVersion = Kernel::VERSION;
41
        $latestRelease = $this->getLatestVersion($currentBranch); // eg. 2.0.12
42
43
        if (version_compare($currentVersion, $latestRelease) < 0) {
44
            return new Warning(sprintf('There is a new release - update to %s from %s.', $latestRelease, $currentVersion));
45
        }
46
47
        return new Success(sprintf('Your current Symfony branch reaches it\'s end of life in %s.', $endOfLife));
48
    }
49
50
    public function getLabel()
51
    {
52
        return 'Symfony version';
53
    }
54
55
    /**
56
     * @param string $branch
57
     *
58
     * @return string
59
     *
60
     * @throws \Exception
61
     */
62
    private function getLatestVersion($branch)
63
    {
64
        $response = $this->getResponseAndDecode(self::PACKAGIST_URL);
65
66
        if (!isset($response['package']['versions'])) {
67
            throw new \Exception('Invalid response from packagist.');
68
        }
69
70
        $branch = 'v'.$branch;
71
72
        // filter out branches and versions without current minor version
73
        $versions = array_filter(
74
            $response['package']['versions'],
75
            function ($value) use ($branch) {
76
                $value = $value['version'];
77
78
                if (stripos($value, 'PR') || stripos($value, 'RC') && stripos($value, 'BETA')) {
79
                    return false;
80
                }
81
82
                return 0 === strpos($value, $branch);
83
            }
84
        );
85
86
        // just get versions
87
        $versions = array_keys($versions);
88
89
        // sort tags
90
        usort($versions, 'version_compare');
91
92
        // reverse to ensure latest is first
93
        $versions = array_reverse($versions);
94
95
        return str_replace('v', '', $versions[0]);
96
    }
97
98
    /**
99
     * @param $url
100
     *
101
     * @return array
102
     *
103
     * @throws \Exception
104
     */
105
    private function getResponseAndDecode($url)
106
    {
107
        if (class_exists(HttpClient::class)) {
108
            return HttpClient::create()->request('GET', $url)->toArray();
109
        }
110
111
        $opts = [
112
            'http' => [
113
                'method' => 'GET',
114
                'header' => "User-Agent: LiipMonitorBundle\r\n",
115
            ],
116
        ];
117
118
        $array = json_decode(file_get_contents($url, false, stream_context_create($opts)), true);
119
120
        if (empty($array)) {
121
            throw new \Exception(sprintf('Invalid response from "%s".', $url));
122
        }
123
124
        return $array;
125
    }
126
}
127