ApplicationInformationService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace App\Services;
4
5
use App\Application;
6
use Exception;
7
use GuzzleHttp\Client;
8
use Illuminate\Contracts\Cache\Repository as Cache;
9
use Illuminate\Log\Logger;
10
11
class ApplicationInformationService
12
{
13
    private const CACHE_KEY = 'latestKoelVersion';
14
15
    private $client;
16
    private $cache;
17
    private $logger;
18
19 1
    public function __construct(Client $client, Cache $cache, Logger $logger)
20
    {
21 1
        $this->client = $client;
22 1
        $this->cache = $cache;
23 1
        $this->logger = $logger;
24 1
    }
25
26
    /**
27
     * Get the latest version number of Koel from GitHub.
28
     */
29 1
    public function getLatestVersionNumber(): string
30
    {
31
        return $this->cache->remember(self::CACHE_KEY, 1 * 24 * 60, function (): string {
32
            try {
33 1
                return json_decode(
34 1
                    $this->client->get('https://api.github.com/repos/phanan/koel/tags')->getBody()
35 1
                )[0]->name;
36
            } catch (Exception $e) {
37
                $this->logger->error($e);
38
39
                return Application::KOEL_VERSION;
40
            }
41 1
        });
42
    }
43
}
44