Skip to content

Commit f4026ca

Browse files
authored
Extract method and rename property refactorings (#1)
Also add another exception case for when the token variable is empty.
1 parent 3b88f40 commit f4026ca

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

src/GitlabApiClient.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ final class GitlabApiClient
2525
{
2626
private const API_URL = 'https://%s/api/v4/ci/lint';
2727

28-
private string $apiToken;
28+
private string $gitlabToken;
2929

30-
private string $configFile;
30+
private string $gitlabFile;
3131

3232
private string $gitlabInstance;
3333

@@ -36,8 +36,8 @@ final class GitlabApiClient
3636
*/
3737
public function __construct(array $config)
3838
{
39-
$this->configFile = $config['gitlab_file'];
40-
$this->apiToken = $config['api_token'];
39+
$this->gitlabToken = $config['api_token'];
40+
$this->gitlabFile = $config['gitlab_file'];
4141
$this->gitlabInstance = $config['gitlab_url'];
4242
}
4343

@@ -52,28 +52,19 @@ public function __construct(array $config)
5252
*/
5353
public function lint(): array
5454
{
55-
$httpClient = HttpClient::create();
56-
57-
$yamlEncoder = new YamlEncoder();
58-
$jsonEncoder = new JsonEncoder();
59-
60-
$fileContents = (string) file_get_contents($this->configFile);
61-
62-
$jsonData = $jsonEncoder->encode(
63-
$yamlEncoder->decode($fileContents, YamlEncoder::FORMAT),
64-
JsonEncoder::FORMAT
65-
);
55+
if (strlen($this->gitlabToken) === 0) {
56+
throw GitlabLinterException::missingToken();
57+
}
6658

67-
$jsonData = str_replace('\\', '\\\\', $jsonData);
68-
$jsonData = str_replace('"', '\"', $jsonData);
59+
$httpClient = HttpClient::create();
6960

7061
$url = sprintf(self::API_URL, $this->gitlabInstance);
7162
$response = $httpClient->request('POST', $url, [
7263
'headers' => [
7364
'Content-Type' => 'application/json',
74-
'PRIVATE-TOKEN' => $this->apiToken,
65+
'PRIVATE-TOKEN' => $this->gitlabToken,
7566
],
76-
'body' => sprintf('{"content": "%s"}', $jsonData),
67+
'body' => sprintf('{"content": "%s"}', $this->yamlConfigEncodedAsJson()),
7768
]);
7869

7970
if ($response->getStatusCode() === 401) {
@@ -86,4 +77,21 @@ public function lint(): array
8677

8778
return $response->toArray();
8879
}
80+
81+
private function yamlConfigEncodedAsJson(): string
82+
{
83+
$fileContents = (string) file_get_contents($this->gitlabFile);
84+
85+
$yamlEncoder = new YamlEncoder();
86+
$jsonEncoder = new JsonEncoder();
87+
88+
$jsonData = $jsonEncoder->encode(
89+
$yamlEncoder->decode($fileContents, YamlEncoder::FORMAT),
90+
JsonEncoder::FORMAT
91+
);
92+
93+
$jsonData = str_replace('\\', '\\\\', $jsonData);
94+
95+
return str_replace('"', '\"', $jsonData);
96+
}
8997
}

src/GitlabLinterException.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ private function __construct(string $message)
1919
parent::__construct($message);
2020
}
2121

22+
public static function missingToken(): self
23+
{
24+
return new self('Your Gitlab API token cannot be read!');
25+
}
26+
2227
public static function unauthorized(): self
2328
{
24-
return new self('Your token has not been authorized by Gitlab!');
29+
return new self('Your Gitlab API token has not been authorized by Gitlab!');
2530
}
2631

2732
public static function error500(): self
@@ -31,6 +36,6 @@ public static function error500(): self
3136

3237
public static function fileNotFound(string $file): self
3338
{
34-
return new self(sprintf('Cannot find the Gitlab file "%s"', $file));
39+
return new self(sprintf('Cannot find the Gitlab CI file "%s"', $file));
3540
}
3641
}

tests/GitlabLintTaskTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,25 @@ public function test_handles_missing_file(): void
4848
$sut->run($context);
4949
}
5050

51-
public function test_handles_authentication_error(): void
51+
public function test_handles_missing_token(): void
5252
{
5353
[
5454
$context,
5555
$sut
5656
] = $this->buildTask(self::VALID_GITLAB_CI_YML, '');
5757

58+
$this->expectExceptionObject(GitlabLinterException::missingToken());
59+
60+
$sut->run($context);
61+
}
62+
63+
public function test_handles_authentication_error(): void
64+
{
65+
[
66+
$context,
67+
$sut
68+
] = $this->buildTask(self::VALID_GITLAB_CI_YML, 'random-token-string');
69+
5870
$this->expectExceptionObject(GitlabLinterException::unauthorized());
5971

6072
$sut->run($context);

0 commit comments

Comments
 (0)