Passed
Push — master ( d2c294...390a75 )
by
unknown
24:38 queued 06:53
created

MolliePluginLatestVersionChecker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
c 1
b 0
f 0
dl 0
loc 38
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMolliePluginLatestVersion() 0 9 2
A __construct() 0 6 1
A checkLatestVersion() 0 11 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Checker\Version;
13
14
use GuzzleHttp\ClientInterface;
15
use GuzzleHttp\Exception\GuzzleException;
16
use GuzzleHttp\Psr7\Uri;
17
use Psr\Http\Message\UriInterface;
18
19
final class MolliePluginLatestVersionChecker implements MolliePluginLatestVersionCheckerInterface
20
{
21
    /** @var ClientInterface */
22
    private $client;
23
24
    /** @var UriInterface */
25
    private $hubUri;
26
27
    public function __construct(
28
        ClientInterface $client,
29
        string $hubUri
30
    ) {
31
        $this->client = $client;
32
        $this->hubUri = new Uri($hubUri);
33
    }
34
35
    public function checkLatestVersion(): ?string
36
    {
37
        try {
38
            $hubResponse = $this->client->request('GET', $this->hubUri);
39
        } catch (GuzzleException $exception) {
40
            return null;
41
        }
42
43
        $hubResponse = json_decode($hubResponse->getBody()->getContents(), true);
44
45
        return $this->getMolliePluginLatestVersion($hubResponse);
46
    }
47
48
    private function getMolliePluginLatestVersion(array $data): ?string
49
    {
50
        $latestVersion = end($data['packages']['bitbag/mollie-plugin']);
51
52
        if (isset($latestVersion['version'])) {
53
            return $latestVersion['version'];
54
        }
55
56
        return null;
57
    }
58
}
59