Passed
Pull Request — master (#74)
by
unknown
07:06
created

getMolliePluginLatestVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
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
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusMolliePlugin\Checker\Version;
14
15
use GuzzleHttp\ClientInterface;
16
use GuzzleHttp\Exception\GuzzleException;
17
use GuzzleHttp\Psr7\Uri;
18
use Psr\Http\Message\UriInterface;
19
20
final class MolliePluginLatestVersionChecker implements MolliePluginLatestVersionCheckerInterface
21
{
22
    /** @var ClientInterface */
23
    private $client;
24
25
    /** @var UriInterface */
26
    private $hubUri;
27
28
    public function __construct(
29
        ClientInterface $client,
30
        string $hubUri
31
    ) {
32
        $this->client = $client;
33
        $this->hubUri = new Uri($hubUri);
34
    }
35
36
    public function checkLatestVersion(): ?string
37
    {
38
        try {
39
            $hubResponse = $this->client->request('GET', $this->hubUri);
40
        } catch (GuzzleException $exception) {
41
            return null;
42
        }
43
        
44
        $hubResponse = json_decode($hubResponse->getBody()->getContents(), true);
45
46
        return $this->getMolliePluginLatestVersion($hubResponse);
47
    }
48
49
    private function getMolliePluginLatestVersion(array $data): ?string
50
    {
51
        $latestVersion = end($data['packages']['bitbag/mollie-plugin']);
52
53
        if (isset($latestVersion['version'])) {
54
            return $latestVersion['version'];
55
        }
56
57
        return null;
58
    }
59
}
60