Passed
Push — master ( 7be10f...68f6f3 )
by Mikołaj
02:43
created

ArtifactsController::artifactInfoAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 3
dl 0
loc 23
ccs 0
cts 21
cp 0
crap 6
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
namespace Wulkanowy\BitriseRedirector\Controller;
4
5
use GuzzleHttp\Client;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\Cache\Simple\FilesystemCache;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Wulkanowy\BitriseRedirector\Service\ArtifactsService;
11
use Wulkanowy\BitriseRedirector\Service\BuildsService;
12
use Wulkanowy\BitriseRedirector\Service\RequestFailedException;
13
14
class ArtifactsController extends Controller
15
{
16
    /**
17
     * @var BuildsService
18
     */
19
    private $builds;
20
21
    /**
22
     * @var ArtifactsService
23
     */
24
    private $artifacts;
25
    /**
26
     * @var Client
27
     */
28
    private $client;
29
30
    /**
31
     * @var FilesystemCache
32
     */
33
    private $cache;
34
35
    public function __construct(Client $client, BuildsService $builds, ArtifactsService $artifacts)
36
    {
37
        $this->builds = $builds;
38
        $this->artifacts = $artifacts;
39
        $this->client = $client;
40
        $this->cache = new FilesystemCache();
41
    }
42
43
    /**
44
     * @param string $slug
45
     * @param string $branch
46
     *
47
     * @throws \RuntimeException
48
     * @throws RequestFailedException
49
     * @throws \Psr\SimpleCache\InvalidArgumentException
50
     *
51
     * @return JsonResponse
52
     */
53
    public function listAction(string $slug, string $branch): JsonResponse
54
    {
55
        return $this->json($this->artifacts->getArtifactsListByBranch($this->client, $branch, $slug));
56
    }
57
58
    /**
59
     * @param string $slug
60
     * @param string $branch
61
     * @param string $artifact
62
     *
63
     * @throws \RuntimeException
64
     * @throws RequestFailedException
65
     * @throws \Psr\SimpleCache\InvalidArgumentException
66
     *
67
     * @return RedirectResponse
68
     */
69
    public function artifactAction(string $slug, string $branch, string $artifact): RedirectResponse
70
    {
71
        $res = $this->getArtifactJson($slug, $branch, $artifact);
72
73
        return $this->redirect($res->public_install_page_url ?: $res->expiring_download_url);
74
    }
75
76
    /**
77
     * @param string $slug
78
     * @param string $branch
79
     * @param string $artifact
80
     *
81
     * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
82
     * @throws \InvalidArgumentException
83
     * @throws \RuntimeException
84
     * @throws RequestFailedException
85
     * @throws \Psr\SimpleCache\InvalidArgumentException
86
     *
87
     * @return JsonResponse
88
     */
89
    public function artifactInfoAction(string $slug, string $branch, string $artifact): JsonResponse
90
    {
91
        $infoTag = 'artifact.'.$branch.'.'.$artifact.'.json';
92
93
        if ($this->cache->has($infoTag)) {
94
            $info = $this->cache->get($infoTag);
95
        } else {
96
            $info = $this->getArtifactJson($slug, $branch, $artifact);
97
            $this->cache->set($infoTag, $info, 60);
98
        }
99
100
        return $this->json(
101
            [
102
                'build_number'            => $info->build_number,
103
                'commit_view_url'         => $info->commit_view_url,
104
                'expiring_download_url'   => $info->expiring_download_url,
105
                'file_size_bytes'         => $info->file_size_bytes,
106
                'finished_at'             => $info->finished_at,
107
                'public_install_page_url' => $info->public_install_page_url,
108
            ],
109
            200,
110
            [
111
                'Access-Control-Allow-Origin' => $this->container->getParameter('cors_origin'),
112
            ]
113
        );
114
    }
115
116
    /**
117
     * @param string $slug
118
     * @param string $branch
119
     * @param string $artifact
120
     *
121
     * @throws \RuntimeException
122
     * @throws RequestFailedException
123
     * @throws \Psr\SimpleCache\InvalidArgumentException
124
     *
125
     * @return \stdClass
126
     */
127
    public function getArtifactJson(string $slug, string $branch, string $artifact): \stdClass
128
    {
129
        $buildInfo = $this->builds->getLastBuildInfoByBranch($this->client, $branch, $slug);
130
        $artifacts = $this->artifacts->getArtifactsListByBranch($this->client, $branch, $slug);
131
        $build = $this->artifacts->getArtifactByFilename($artifacts, $artifact);
132
133
        if (null === $build) {
134
            throw new RequestFailedException('Artifact not found.', 404);
135
        }
136
137
        $response = $this->client->get('apps/'.$slug.'/builds/'.$buildInfo['slug'].'/artifacts/'.$build->slug)
138
            ->getBody()->getContents();
139
140
        $info = \json_decode($response)->data;
141
142
        $info->build_number = $buildInfo['build_number'];
143
        $info->commit_view_url = $buildInfo['commit_view_url'];
144
        $info->finished_at = $buildInfo['finished_at'];
145
146
        return $info;
147
    }
148
}
149