InfoService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
eloc 38
c 1
b 1
f 0
dl 0
loc 78
ccs 0
cts 19
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadVersionFromGameap() 0 22 3
A loadVersionFromGithub() 0 24 4
A __construct() 0 4 1
A latestRelease() 0 9 2
1
<?php
2
3
namespace Gameap\Services;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\GuzzleException;
7
use GuzzleHttp\RequestOptions;
8
use Illuminate\Http\Response;
9
use Psr\Log\LoggerInterface;
10
11
class InfoService
12
{
13
    private const GAMEAP_LATEST_VERSION_URI = 'https://www.gameap.com/gameap_version.txt';
14
    private const GITHUB_LATEST_VERSION_URI = 'https://api.github.com/repos/et-nik/gameap/releases/latest';
15
16
    /** @var Client */
17
    private $client;
18
19
    /** @var LoggerInterface */
20
    private $logger;
21
22
    private $timeoutInSeconds = 1;
23
24
    public function __construct(Client $client, LoggerInterface $logger)
25
    {
26
        $this->client = $client;
27
        $this->logger = $logger;
28
    }
29
30
    public function latestRelease(): string
31
    {
32
        $version = $this->loadVersionFromGameap();
33
34
        if ($version === '') {
35
            $version = $this->loadVersionFromGithub();
36
        }
37
38
        return $version;
39
    }
40
41
    private function loadVersionFromGameap(): string
42
    {
43
        try {
44
            $res = $this->client->get(
45
                self::GAMEAP_LATEST_VERSION_URI,
46
                [
47
                    RequestOptions::TIMEOUT => $this->timeoutInSeconds,
48
                    RequestOptions::CONNECT_TIMEOUT => $this->timeoutInSeconds,
49
                ]
50
            );
51
        } catch (GuzzleException $e) {
52
            $this->logger->error($e->getMessage());
53
            return '';
54
        }
55
56
        if ($res->getStatusCode() === Response::HTTP_OK) {
57
            $lines  = explode("\n", $res->getBody()->getContents());
58
            $parts  = explode(': ', $lines[0]);
59
            return $parts[1];
60
        }
61
62
        return '';
63
    }
64
65
    private function loadVersionFromGithub(): string
66
    {
67
        try {
68
            $res = $this->client->get(
69
                self::GITHUB_LATEST_VERSION_URI,
70
                [
71
                    RequestOptions::TIMEOUT => $this->timeoutInSeconds,
72
                    RequestOptions::CONNECT_TIMEOUT => $this->timeoutInSeconds,
73
                ]
74
            );
75
        } catch (GuzzleException $e) {
76
            $this->logger->error($e->getMessage());
77
            return '';
78
        }
79
80
        if ($res->getStatusCode() === Response::HTTP_OK) {
81
            $result = json_decode($res->getBody()->getContents());
82
83
            if (!empty($result->tag_name)) {
84
                return $result->tag_name;
85
            }
86
        }
87
88
        return '';
89
    }
90
}
91