1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wulkanowy\BitriseRedirector\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
6
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
7
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
8
|
|
|
use Wulkanowy\BitriseRedirector\Service\ArtifactsService; |
9
|
|
|
use Wulkanowy\BitriseRedirector\Service\RequestFailedException; |
10
|
|
|
|
11
|
|
|
class ArtifactsController extends AbstractController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $corsRule; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ArtifactsService |
20
|
|
|
*/ |
21
|
|
|
private $artifacts; |
22
|
|
|
|
23
|
|
|
public function __construct($corsRule, ArtifactsService $artifacts) |
24
|
|
|
{ |
25
|
|
|
$this->corsRule = $corsRule; |
26
|
|
|
$this->artifacts = $artifacts; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $slug |
31
|
|
|
* @param string $branch |
32
|
|
|
* |
33
|
|
|
* @throws \RuntimeException |
34
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
35
|
|
|
* |
36
|
|
|
* @return JsonResponse |
37
|
|
|
*/ |
38
|
|
|
public function listAction(string $slug, string $branch): JsonResponse |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
|
|
return $this->json($this->artifacts->getArtifactsListByBranch($branch, $slug)); |
42
|
|
|
} catch (RequestFailedException $e) { |
43
|
|
|
return $this->getErrorResponse($e); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $slug |
49
|
|
|
* @param string $branch |
50
|
|
|
* @param string $filename |
51
|
|
|
* |
52
|
|
|
* @throws \RuntimeException |
53
|
|
|
* @throws RequestFailedException |
54
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
55
|
|
|
* |
56
|
|
|
* @return RedirectResponse |
57
|
|
|
*/ |
58
|
|
|
public function artifactAction(string $slug, string $branch, string $filename): RedirectResponse |
59
|
|
|
{ |
60
|
|
|
return $this->getArtifact($slug, $branch, $filename); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $slug |
65
|
|
|
* @param string $branch |
66
|
|
|
* @param int $index |
67
|
|
|
* |
68
|
|
|
* @throws RequestFailedException |
69
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
70
|
|
|
* |
71
|
|
|
* @return RedirectResponse |
72
|
|
|
*/ |
73
|
|
|
public function artifactIndexAction(string $slug, string $branch, int $index): RedirectResponse |
74
|
|
|
{ |
75
|
|
|
return $this->getArtifact($slug, $branch, $index); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $slug |
80
|
|
|
* @param string $branch |
81
|
|
|
* @param $key |
82
|
|
|
* |
83
|
|
|
* @throws RequestFailedException |
84
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
85
|
|
|
* |
86
|
|
|
* @return RedirectResponse |
87
|
|
|
*/ |
88
|
|
|
private function getArtifact(string $slug, string $branch, $key): RedirectResponse |
89
|
|
|
{ |
90
|
|
|
$artifacts = $this->artifacts->getArtifactsListByBranch($branch, $slug); |
91
|
|
|
$artifact = $this->artifacts->getArtifact($artifacts, $key); |
92
|
|
|
$res = $this->artifacts->getArtifactInfo($slug, $branch, $artifact); |
93
|
|
|
|
94
|
|
|
return $this->redirect($res->public_install_page_url ?: $res->expiring_download_url); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $slug |
99
|
|
|
* @param string $branch |
100
|
|
|
* @param string $filename |
101
|
|
|
* |
102
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
103
|
|
|
* @throws \InvalidArgumentException |
104
|
|
|
* @throws \RuntimeException |
105
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
106
|
|
|
* |
107
|
|
|
* @return JsonResponse |
108
|
|
|
*/ |
109
|
|
|
public function artifactInfoAction(string $slug, string $branch, string $filename): JsonResponse |
110
|
|
|
{ |
111
|
|
|
return $this->getArtifactsInfoResponse($slug, $branch, $filename); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $slug |
116
|
|
|
* @param string $branch |
117
|
|
|
* @param int $index |
118
|
|
|
* |
119
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
120
|
|
|
* |
121
|
|
|
* @return JsonResponse |
122
|
|
|
*/ |
123
|
|
|
public function artifactInfoIndexAction(string $slug, string $branch, int $index): JsonResponse |
124
|
|
|
{ |
125
|
|
|
return $this->getArtifactsInfoResponse($slug, $branch, $index); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $slug |
130
|
|
|
* @param string $branch |
131
|
|
|
* @param $key |
132
|
|
|
* |
133
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
134
|
|
|
* |
135
|
|
|
* @return JsonResponse |
136
|
|
|
*/ |
137
|
|
|
private function getArtifactsInfoResponse(string $slug, string $branch, $key): JsonResponse |
138
|
|
|
{ |
139
|
|
|
try { |
140
|
|
|
$artifacts = $this->artifacts->getArtifactsListByBranch($branch, $slug); |
141
|
|
|
$artifact = $this->artifacts->getArtifact($artifacts, $key); |
142
|
|
|
$info = $this->artifacts->getArtifactInfo($slug, $branch, $artifact); |
143
|
|
|
} catch (RequestFailedException $e) { |
144
|
|
|
return $this->getErrorResponse($e); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->json( |
148
|
|
|
[ |
149
|
|
|
'error' => null, |
150
|
|
|
'build_number' => $info->build_number, |
151
|
|
|
'commit_view_url' => $info->commit_view_url, |
152
|
|
|
'expiring_download_url' => $info->expiring_download_url, |
153
|
|
|
'file_size_bytes' => $info->file_size_bytes, |
154
|
|
|
'finished_at' => $info->finished_at, |
155
|
|
|
'public_install_page_url' => $info->public_install_page_url, |
156
|
|
|
], |
157
|
|
|
200, |
158
|
|
|
[ |
159
|
|
|
'Access-Control-Allow-Origin' => $this->corsRule, |
160
|
|
|
] |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function getErrorResponse(RequestFailedException $e): JsonResponse |
165
|
|
|
{ |
166
|
|
|
return $this->json([ |
167
|
|
|
'error' => [ |
168
|
|
|
'message' => $e->getMessage(), |
169
|
|
|
], |
170
|
|
|
]); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|