ArtifactsService   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 23.26%

Importance

Changes 6
Bugs 3 Features 0
Metric Value
eloc 41
c 6
b 3
f 0
dl 0
loc 118
ccs 10
cts 43
cp 0.2326
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getArtifact() 0 7 2
A getArtifactInfo() 0 25 3
A getArtifactByIndex() 0 3 1
A __construct() 0 5 1
A getArtifactByFilename() 0 9 3
A getArtifactsListByBranch() 0 21 3
1
<?php
2
3
namespace Wulkanowy\BitriseRedirector\Service;
4
5
use GuzzleHttp\Client;
6
use function is_int;
7
use function json_decode;
8
use Psr\SimpleCache\InvalidArgumentException;
9
use RuntimeException;
10
use stdClass;
11
use Symfony\Component\Cache\Simple\FilesystemCache;
12
13
class ArtifactsService
14
{
15
    /**
16
     * @var Client
17
     */
18
    private $client;
19
20
    /**
21
     * @var BuildsService
22
     */
23
    private $builds;
24
25
    /**
26
     * @var FilesystemCache
27
     */
28
    private $cache;
29
30 1
    public function __construct(Client $client, BuildsService $builds)
31
    {
32 1
        $this->client = $client;
33 1
        $this->builds = $builds;
34 1
        $this->cache = new FilesystemCache();
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Cache\Simple\FilesystemCache has been deprecated: since Symfony 4.3, use FilesystemAdapter and type-hint for CacheInterface instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

34
        $this->cache = /** @scrutinizer ignore-deprecated */ new FilesystemCache();
Loading history...
35 1
    }
36
37
    /**
38
     * @param string $slug
39
     * @param string $branch
40
     *
41
     * @throws RuntimeException
42
     * @throws RequestFailedException
43
     * @throws InvalidArgumentException
44
     *
45
     * @return array
46
     */
47
    public function getArtifactsListByBranch(string $branch, string $slug) : array
48
    {
49
        $tag = 'artifacts.'.$slug.'.'.str_replace('/', '-', $branch);
50
51
        $lastBuild = $this->builds->getLastBuildInfoByBranch($branch, $slug);
52
53
        if ($this->cache->has($tag)) {
54
            $response = $this->cache->get($tag);
55
        } else {
56
            $response = $this->client->get('apps/'.$slug.'/builds/'.$lastBuild['slug'].'/artifacts')
57
                ->getBody()->getContents();
58
            $this->cache->set($tag, $response, 60);
59
        }
60
61
        $artifacts = json_decode($response)->data;
62
63
        foreach ($artifacts as $key => $item) {
64
            $item->build_slug = $lastBuild['slug'];
65
        }
66
67
        return $artifacts;
68
    }
69
70
    /**
71
     * @param string   $slug
72
     * @param string   $branch
73
     * @param stdClass $artifact
74
     *
75
     * @throws RuntimeException
76
     * @throws RequestFailedException
77
     * @throws InvalidArgumentException
78
     *
79
     * @return stdClass
80
     */
81
    public function getArtifactInfo(string $slug, string $branch, ?stdClass $artifact): stdClass
82
    {
83
        $buildInfo = $this->builds->getLastBuildInfoByBranch($branch, $slug);
84
85
        if (null === $artifact) {
86
            throw new RequestFailedException('Artifact not found.', 404);
87
        }
88
89
        $infoTag = 'artifact.'.str_replace('/', '-', $branch).'.'.$artifact->title.'.json';
90
91
        if ($this->cache->has($infoTag)) {
92
            $response = $this->cache->get($infoTag);
93
        } else {
94
            $response = $this->client->get('apps/'.$slug.'/builds/'.$buildInfo['slug'].'/artifacts/'.$artifact->slug)
95
                ->getBody()->getContents();
96
            $this->cache->set($infoTag, $response, 60);
97
        }
98
99
        $info = json_decode($response)->data;
100
101
        $info->build_number = $buildInfo['build_number'];
102
        $info->commit_view_url = $buildInfo['commit_view_url'];
103
        $info->finished_at = $buildInfo['finished_at'];
104
105
        return $info;
106
    }
107
108
    public function getArtifact(array $artifacts, $key): ?stdClass
109
    {
110
        if (is_int($key)) {
111
            return $this->getArtifactByIndex($artifacts, $key);
112
        }
113
114
        return $this->getArtifactByFilename($artifacts, $key);
115
    }
116
117 1
    public function getArtifactByFilename(array $artifacts, string $filename): ?stdClass
118
    {
119 1
        foreach ($artifacts as $key => $item) {
120 1
            if ($filename === $item->title) {
121 1
                return $item;
122
            }
123
        }
124
125 1
        return null;
126
    }
127
128
    public function getArtifactByIndex(array $artifacts, int $index): ?stdClass
129
    {
130
        return $artifacts[$index] ?? null;
131
    }
132
}
133