Skip to content

Commit 2694eba

Browse files
committed
chore(): Clean up
1 parent e0952dc commit 2694eba

14 files changed

+219
-289
lines changed

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
"nunomaduro/termwind": "^1.5",
3131
"composer-runtime-api": "^2.2.2",
3232
"ahinkle/packagist-latest-version": "^2.0"
33+
},
34+
"require-dev": {
35+
"roave/security-advisories": "dev-latest"
3336
}
3437
}

src/Command/CheckUpdateCommand.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Artemeon\M2G\Command;
46

57
use Artemeon\M2G\Helper\VersionHelper;
68

9+
use Exception;
10+
use JsonException;
11+
712
use function Termwind\{render};
813

914
class CheckUpdateCommand extends Command
1015
{
11-
protected function configure()
12-
{
13-
$this->setName('check_update')
14-
->setDescription('Checks whether a new version is available');
15-
}
16+
protected string $signature = 'check_update';
17+
protected ?string $description = 'Checks whether a new version is available';
18+
protected bool $hidden = true;
1619

17-
protected function handle(): int
20+
/**
21+
* @throws JsonException
22+
* @throws Exception
23+
*/
24+
public function __invoke(): int
1825
{
1926
$currentVersion = VersionHelper::fetchVersion();
2027
$latestVersion = VersionHelper::latestVersion();
@@ -38,6 +45,6 @@ protected function handle(): int
3845
HTML);
3946
}
4047

41-
return 0;
48+
return self::SUCCESS;
4249
}
4350
}

src/Command/Command.php

+14-102
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,39 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Artemeon\M2G\Command;
46

57
use Artemeon\M2G\Config\ConfigReader;
6-
use Exception;
7-
use Symfony\Component\Console\Helper\QuestionHelper;
8-
use Symfony\Component\Console\Input\InputInterface;
9-
use Symfony\Component\Console\Output\OutputInterface;
10-
use Symfony\Component\Console\Question\Question;
11-
12-
use function Termwind\render;
138

14-
abstract class Command extends \Symfony\Component\Console\Command\Command
9+
class Command extends \Artemeon\Console\Command
1510
{
16-
protected InputInterface $input;
17-
protected OutputInterface $output;
18-
19-
/**
20-
* @throws Exception
21-
*/
22-
protected function execute(InputInterface $input, OutputInterface $output): int
11+
final protected function header(): void
2312
{
24-
$this->input = $input;
25-
$this->output = $output;
26-
27-
return $this->handle();
28-
}
29-
30-
protected function arguments(): array
31-
{
32-
return $this->input->getArguments();
33-
}
34-
35-
protected function argument(string $name)
36-
{
37-
return $this->input->getArgument($name);
38-
}
39-
40-
protected function options(): array
41-
{
42-
return $this->input->getOptions();
43-
}
44-
45-
protected function option(string $name)
46-
{
47-
return $this->input->getOption($name);
48-
}
49-
50-
protected function header(): void
51-
{
52-
$this->success(
13+
$this->output->write(
5314
'
54-
__ __ _ _ <comment>____</comment> ____ _ _ _ _ _
55-
| \/ | __ _ _ __ | |_ (_) ___ <comment>|___ \ </comment> / ___|(_)| |_ | | | | _ _ | |__
56-
| |\/| | / _` || \'_ \ | __|| |/ __| <comment>__) |</comment> | | _ | || __|| |_| || | | || \'_ \
15+
__ __ _ _ <comment>____</comment> ____ _ _ _ _ _
16+
| \/ | __ _ _ __ | |_ (_) ___ <comment>|___ \ </comment> / ___|(_)| |_ | | | | _ _ | |__
17+
| |\/| | / _` || \'_ \ | __|| |/ __| <comment>__) |</comment> | | _ | || __|| |_| || | | || \'_ \
5718
| | | || (_| || | | || |_ | |\__ \ <comment>/ __/</comment> | |_| || || |_ | _ || |_| || |_) |
58-
|_| |_| \__,_||_| |_| \__||_||___/ <comment>|_____|</comment> \____||_| \__||_| |_| \__,_||_.__/
59-
60-
'
61-
);
62-
}
63-
64-
protected function ask(string $question, string $default = null, bool $hidden = false)
65-
{
66-
/** @var QuestionHelper $helper */
67-
$helper = $this->getHelper('question');
68-
$trailingSpace = str_ends_with($question, ' ') ? '' : ' ';
69-
$question = new Question($question . $trailingSpace, $default);
70-
$question->setHidden($hidden);
71-
72-
return $helper->ask($this->input, $this->output, $question);
73-
}
19+
|_| |_| \__,_||_| |_| \__||_||___/ <comment>|_____|</comment> \____||_| \__||_| |_| \__,_||_.__/
7420
75-
protected function secret(string $question, string $default = null)
76-
{
77-
return $this->ask($question, $default, true);
78-
}
79-
80-
protected function info(string $message)
81-
{
82-
$this->output->writeln($message);
83-
}
8421
85-
protected function error(string $message)
86-
{
87-
render(
88-
<<<HTML
89-
<div class="my-1 ml-1 px-1 bg-red-400 text-white">
90-
$message
91-
</div>
92-
HTML
93-
);
94-
}
95-
96-
protected function success(string $message)
97-
{
98-
$this->output->writeln("<info>$message</info>");
99-
}
100-
101-
protected function warn(string $message)
102-
{
103-
render(
104-
<<<HTML
105-
<div class="ml-1 px-1 bg-yellow-500 text-gray-900">
106-
<strong>! $message !</strong>
107-
</div>
108-
HTML
22+
'
10923
);
11024
}
11125

112-
protected function checkConfig(): void
26+
final protected function checkConfig(): void
11327
{
11428
$config = (new ConfigReader())->read();
11529

11630
if (!$config) {
117-
$this->info('');
31+
$this->newLine();
11832
$this->warn('You have not configured mantis2github yet');
11933
$this->warn('Please run "mantis2github configure" to get started');
120-
$this->info('');
34+
$this->newLine();
12135

122-
exit(1);
36+
exit(self::INVALID);
12337
}
12438
}
125-
126-
abstract protected function handle(): int;
12739
}

src/Command/ConfigurationCommand.php

+24-33
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Artemeon\M2G\Command;
46

57
use Artemeon\M2G\Config\ConfigReader;
6-
use Symfony\Component\Console\Input\InputArgument;
78
use Symfony\Component\Yaml\Yaml;
89

910
use function Termwind\{render, terminal};
1011

1112
class ConfigurationCommand extends Command
1213
{
14+
protected string $signature = 'configure {file? : The config.yaml to use for setting up the tool.}';
15+
protected ?string $description = 'Configure the tool';
16+
1317
protected string $configPath = __DIR__ . '/../../../config.yaml';
1418
protected array $config = [];
1519

16-
protected function configure()
17-
{
18-
$this->setName('configure')
19-
->setDescription('Configure the tool')
20-
->addArgument('file', InputArgument::OPTIONAL, 'The config.yaml to use for setting up the tool.');
21-
}
22-
23-
protected function handle(): int
20+
public function __invoke(): int
2421
{
2522
$this->readExistingConfigFromPath();
2623

@@ -34,9 +31,10 @@ protected function handle(): int
3431
$this->warn('Configuration file already exists');
3532
$this->warn('If you continue your configuration will be overwritten');
3633

37-
if ($this->ask("\n Are you sure you want to continue? [Y/n]", 'n') !== 'Y') {
34+
if (!$this->confirm("\n Are you sure you want to continue?")) {
3835
$this->info("\n Alright!\n");
39-
return 1;
36+
37+
return self::INVALID;
4038
}
4139
}
4240

@@ -50,14 +48,12 @@ protected function handle(): int
5048
$this->askForGitHubRepository();
5149
$this->saveConfig();
5250

53-
return 0;
51+
return self::SUCCESS;
5452
}
5553

56-
protected function askForMantisUrl(): void
54+
private function askForMantisUrl(): void
5755
{
58-
$this->info(" Please enter the URL of your Mantis installation (e.g. https://tickets.company.tld):");
59-
60-
$mantisUrl = $this->ask(" >");
56+
$mantisUrl = $this->ask('Please enter the URL of your Mantis installation (e.g. https://tickets.company.tld):');
6157

6258
$parsedUrl = parse_url($mantisUrl);
6359

@@ -72,7 +68,7 @@ protected function askForMantisUrl(): void
7268
$mantisUrl = "{$parsedUrl['scheme']}://{$parsedUrl['host']}$port/";
7369

7470
// Check if something is available on the given URL
75-
// If not, we assume that the URL is wrong
71+
// If not, we assume that the URL is wrong.
7672
$headers = @get_headers($mantisUrl);
7773
if (!$headers || $headers[0] === 'HTTP/1.1 404 Not Found') {
7874
$this->error(
@@ -85,12 +81,10 @@ protected function askForMantisUrl(): void
8581
$this->config['mantisUrl'] = $mantisUrl;
8682
}
8783

88-
protected function askForMantisToken(): void
84+
private function askForMantisToken(): void
8985
{
90-
$this->info("\n Head over to {$this->config['mantisUrl']}api_tokens_page.php, create a new API token,");
91-
$this->info(" and enter the token here:");
92-
93-
$token = $this->secret(" >");
86+
$this->info("Head over to {$this->config['mantisUrl']}api_tokens_page.php and create a new API token.");
87+
$token = $this->secret('Mantis API Token');
9488

9589
if (empty($token)) {
9690
$this->error('The token is empty. Please try again.');
@@ -101,12 +95,11 @@ protected function askForMantisToken(): void
10195
$this->config['mantisToken'] = $token;
10296
}
10397

104-
protected function askForGitHubToken(): void
98+
private function askForGitHubToken(): void
10599
{
106-
$this->info("\n Head over to https://github.com/settings/tokens, create a new personal access token");
107-
$this->info(" with the `repo` scope and enter the token here:");
100+
$this->info("Head over to https://github.com/settings/tokens, create a new personal access token with the `repo` scope.");
108101

109-
$token = $this->secret(" >");
102+
$token = $this->secret("GitHub Token");
110103

111104
if (empty($token)) {
112105
$this->error('The token is empty. Please try again.');
@@ -117,11 +110,9 @@ protected function askForGitHubToken(): void
117110
$this->config['githubToken'] = $token;
118111
}
119112

120-
protected function askForGitHubRepository(): void
113+
private function askForGitHubRepository(): void
121114
{
122-
$this->info("\n Enter the GitHub repository you want to create issues for (e.g. user/repository):");
123-
124-
$repository = $this->ask(" >");
115+
$repository = $this->ask('Enter the GitHub repository you want to create issues for (e.g. user/repository)');
125116

126117
if (empty($repository) || count(explode('/', $repository)) !== 2) {
127118
$this->error("The given repository is invalid.");
@@ -132,7 +123,7 @@ protected function askForGitHubRepository(): void
132123
$this->config['githubRepository'] = $repository;
133124
}
134125

135-
protected function saveConfig(): void
126+
private function saveConfig(): void
136127
{
137128
$stub = file_get_contents(__DIR__ . '/../../stubs/config.yaml.stub');
138129

@@ -153,7 +144,7 @@ protected function saveConfig(): void
153144
$this->success(" Synchronize your first issue by running `mantis2github sync`!\n");
154145
}
155146

156-
protected function readExistingConfigFromPath()
147+
private function readExistingConfigFromPath(): void
157148
{
158149
if (!$this->argument('file')) {
159150
return;
@@ -183,6 +174,6 @@ protected function readExistingConfigFromPath()
183174

184175
$this->saveConfig();
185176

186-
exit(0);
177+
exit(self::SUCCESS);
187178
}
188179
}

0 commit comments

Comments
 (0)