Passed
Push — develop ( 99647f...e3783d )
by Nikita
10:44
created

InfoService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

4 Methods

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