Test Setup Failed
Branch master (4b8153)
by Phan
05:07
created

ApplicationInformationService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getLatestVersionNumber() 0 14 2
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
    public function __construct(Client $client, Cache $cache, Logger $logger)
20
    {
21
        $this->client = $client;
22
        $this->cache = $cache;
23
        $this->logger = $logger;
24
    }
25
26
    /**
27
     * Get the latest version number of Koel from GitHub.
28
     */
29
    public function getLatestVersionNumber(): string
30
    {
31
        return $this->cache->remember(self::CACHE_KEY, 1 * 24 * 60, function (): string {
32
            try {
33
                return json_decode(
34
                    $this->client->get('https://api.github.com/repos/phanan/koel/tags')->getBody()
35
                )[0]->name;
36
            } catch (Exception $e) {
37
                $this->logger->error($e);
38
39
                return Application::KOEL_VERSION;
40
            }
41
        });
42
    }
43
}
44