Passed
Push — master ( 54f0ca...ad575b )
by Stephen
05:18
created

GithubUrl::description()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 18
rs 9.9332
1
<?php
2
3
namespace Sfneal\Dependencies\Utils;
4
5
use Illuminate\Support\Facades\Cache;
6
use Illuminate\Support\Facades\Http;
7
8
class GithubUrl extends DependencyUrl
9
{
10
    /**
11
     * @var Url
12
     */
13
    private $api;
14
15
    /**
16
     * GithubUrl Constructor.
17
     *
18
     * @param  Url  $url
19
     * @param  Url  $api
20
     */
21
    public function __construct(Url $url, Url $api)
22
    {
23
        parent::__construct($url, null);
24
        $this->api = $api;
25
    }
26
27
    /**
28
     * Retrieve the GitHub repo's description.
29
     *
30
     * @return string
31
     */
32
    public function description(): ?string
33
    {
34
        $response = Http::withHeaders([
35
            'Authorization' => 'token '.config('dependencies.github_pat'),
36
        ])
37
        ->get($this->api->get());
38
39
        // Client error
40
        if ($response->clientError() && str_contains($response->json('message'), 'API rate limit exceeded')) {
41
            return null;
42
        }
43
44
        // Cache response
45
        return Cache::remember(
46
            config('dependencies.cache.prefix').':api-responses:'.$this->api->get(),
47
            config('dependencies.cache.ttl'),
48
            function () use ($response) {
49
                return $response->json('description');
50
            }
51
        );
52
    }
53
}
54