Passed
Push — master ( 5a24a3...22f313 )
by Stephen
05:12
created

GithubUrl::workflow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
     * @var string
17
     */
18
    private $githubRepo;
19
20
    /**
21
     * @var array|null Array of global Img Shields params to be passed to SVG requests
22
     */
23
    private $imgShieldGlobals;
24
25
    /**
26
     * GithubUrl Constructor.
27
     *
28
     * @param  string  $githubRepo  GitHub repo name
29
     */
30
    public function __construct(string $githubRepo, array $imgShieldGlobals = null)
31
    {
32
        $this->githubRepo = $githubRepo;
33
        $this->imgShieldGlobals = $imgShieldGlobals;
34
35
        $this->api = Url::from("api.github.com/repos/{$this->githubRepo}");
36
37
        parent::__construct(Url::from("github.com/{$this->githubRepo}"), null);
38
    }
39
40
    /**
41
     * Retrieve the GitHub repo's description.
42
     *
43
     * @return string
44
     */
45
    public function description(): ?string
46
    {
47
        return $this->getApiResponse()['description'];
48
    }
49
50
    /**
51
     * Retrieve the GitHub repo's description.
52
     *
53
     * @return string
54
     */
55
    public function defaultBranch(): string
56
    {
57
        return $this->getApiResponse()['default_branch'] ?? 'master';
58
    }
59
60
    /**
61
     * Retrieve a link to download the GitHub repo zip.
62
     *
63
     * @return string
64
     */
65
    public function download(): string
66
    {
67
        return Url::from("github.com/{$this->githubRepo}/archive/refs/heads/{$this->defaultBranch()}.zip")->get();
68
    }
69
70
    /**
71
     * Display pass/fail status for a GitHub repo's workflow.
72
     *
73
     * @param  string  $name
74
     * @return DependencyUrl
75
     */
76
    public function workflow(string $name): DependencyUrl
77
    {
78
        return new DependencyUrl(
79
            ImgShieldsUrl::from("github/workflow/status/{$this->githubRepo}/{$name}")
80
                ->withGlobalParams($this->imgShieldGlobals)
81
                ->withParams([
82
                    'logo' => 'github',
83
                    'label' => $name,
84
                ])
85
        );
86
    }
87
88
    /**
89
     * Retrieve a cached HTTP response from the GitHub api.
90
     *
91
     * @return ?array
92
     */
93
    private function getApiResponse(): ?array
94
    {
95
        $response = Http::withHeaders([
96
            'Authorization' => 'token '.config('dependencies.github_pat'),
97
        ])
98
            ->get($this->api->get());
99
100
        // Client error
101
        if ($response->clientError() && str_contains($response->json('message'), 'API rate limit exceeded')) {
102
            return null;
103
        }
104
105
        // Cache response
106
        return Cache::remember(
107
            config('dependencies.cache.prefix').':api-responses:'.crc32($this->api->get()),
108
            config('dependencies.cache.ttl'),
109
            function () use ($response): array {
110
                return $response->json();
111
            }
112
        );
113
    }
114
}
115