1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
namespace Artemeon \M2G \Command ;
4
6
5
7
use Artemeon \M2G \Config \ConfigReader ;
6
- use Symfony \Component \Console \Input \InputArgument ;
7
8
use Symfony \Component \Yaml \Yaml ;
8
9
9
10
use function Termwind \{render , terminal };
10
11
11
12
class ConfigurationCommand extends Command
12
13
{
14
+ protected string $ signature = 'configure {file? : The config.yaml to use for setting up the tool.} ' ;
15
+ protected ?string $ description = 'Configure the tool ' ;
16
+
13
17
protected string $ configPath = __DIR__ . '/../../../config.yaml ' ;
14
18
protected array $ config = [];
15
19
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
24
21
{
25
22
$ this ->readExistingConfigFromPath ();
26
23
@@ -34,9 +31,10 @@ protected function handle(): int
34
31
$ this ->warn ('Configuration file already exists ' );
35
32
$ this ->warn ('If you continue your configuration will be overwritten ' );
36
33
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? " ) ) {
38
35
$ this ->info ("\n Alright! \n" );
39
- return 1 ;
36
+
37
+ return self ::INVALID ;
40
38
}
41
39
}
42
40
@@ -50,14 +48,12 @@ protected function handle(): int
50
48
$ this ->askForGitHubRepository ();
51
49
$ this ->saveConfig ();
52
50
53
- return 0 ;
51
+ return self :: SUCCESS ;
54
52
}
55
53
56
- protected function askForMantisUrl (): void
54
+ private function askForMantisUrl (): void
57
55
{
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): ' );
61
57
62
58
$ parsedUrl = parse_url ($ mantisUrl );
63
59
@@ -72,7 +68,7 @@ protected function askForMantisUrl(): void
72
68
$ mantisUrl = "{$ parsedUrl ['scheme ' ]}:// {$ parsedUrl ['host ' ]}$ port/ " ;
73
69
74
70
// 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.
76
72
$ headers = @get_headers ($ mantisUrl );
77
73
if (!$ headers || $ headers [0 ] === 'HTTP/1.1 404 Not Found ' ) {
78
74
$ this ->error (
@@ -85,12 +81,10 @@ protected function askForMantisUrl(): void
85
81
$ this ->config ['mantisUrl ' ] = $ mantisUrl ;
86
82
}
87
83
88
- protected function askForMantisToken (): void
84
+ private function askForMantisToken (): void
89
85
{
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 ' );
94
88
95
89
if (empty ($ token )) {
96
90
$ this ->error ('The token is empty. Please try again. ' );
@@ -101,12 +95,11 @@ protected function askForMantisToken(): void
101
95
$ this ->config ['mantisToken ' ] = $ token ;
102
96
}
103
97
104
- protected function askForGitHubToken (): void
98
+ private function askForGitHubToken (): void
105
99
{
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. " );
108
101
109
- $ token = $ this ->secret (" > " );
102
+ $ token = $ this ->secret ("GitHub Token " );
110
103
111
104
if (empty ($ token )) {
112
105
$ this ->error ('The token is empty. Please try again. ' );
@@ -117,11 +110,9 @@ protected function askForGitHubToken(): void
117
110
$ this ->config ['githubToken ' ] = $ token ;
118
111
}
119
112
120
- protected function askForGitHubRepository (): void
113
+ private function askForGitHubRepository (): void
121
114
{
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) ' );
125
116
126
117
if (empty ($ repository ) || count (explode ('/ ' , $ repository )) !== 2 ) {
127
118
$ this ->error ("The given repository is invalid. " );
@@ -132,7 +123,7 @@ protected function askForGitHubRepository(): void
132
123
$ this ->config ['githubRepository ' ] = $ repository ;
133
124
}
134
125
135
- protected function saveConfig (): void
126
+ private function saveConfig (): void
136
127
{
137
128
$ stub = file_get_contents (__DIR__ . '/../../stubs/config.yaml.stub ' );
138
129
@@ -153,7 +144,7 @@ protected function saveConfig(): void
153
144
$ this ->success (" Synchronize your first issue by running `mantis2github sync`! \n" );
154
145
}
155
146
156
- protected function readExistingConfigFromPath ()
147
+ private function readExistingConfigFromPath (): void
157
148
{
158
149
if (!$ this ->argument ('file ' )) {
159
150
return ;
@@ -183,6 +174,6 @@ protected function readExistingConfigFromPath()
183
174
184
175
$ this ->saveConfig ();
185
176
186
- exit (0 );
177
+ exit (self :: SUCCESS );
187
178
}
188
179
}
0 commit comments