@@ -31,8 +31,8 @@ public function __invoke(): int
3131 $ this ->warn ('Configuration file already exists ' );
3232 $ this ->warn ('If you continue your configuration will be overwritten ' );
3333
34- if (!$ this ->confirm ("\n Are you sure you want to continue?" )) {
35- $ this ->info ("\n Alright!\n" );
34+ if (!$ this ->confirm (' Are you sure you want to continue? ' )) {
35+ $ this ->info (' Alright! ' );
3636
3737 return self ::INVALID ;
3838 }
@@ -53,74 +53,77 @@ public function __invoke(): int
5353
5454 private function askForMantisUrl (): void
5555 {
56- $ mantisUrl = $ this ->ask ('Please enter the URL of your Mantis installation (e.g. https://tickets.company.tld): ' );
56+ $ mantisUrl = $ this ->ask (
57+ label: 'The URL of your Mantis installation ' ,
58+ placeholder: 'E.g. https://tickets.company.tld/ ' ,
59+ required: true ,
60+ validate: function ($ value ) {
61+ $ parsedUrl = parse_url ($ value );
62+
63+ if ($ parsedUrl === false ) {
64+ return 'You must enter a valid URL. ' ;
65+ }
66+
67+ if (!isset ($ parsedUrl ['scheme ' ])) {
68+ return 'The URL must include a scheme like "http://" or "https://". ' ;
69+ }
70+
71+ if (!isset ($ parsedUrl ['host ' ])) {
72+ return 'The URL must include a valid host. ' ;
73+ }
74+
75+ $ port = isset ($ parsedUrl ['port ' ]) ? ': ' . $ parsedUrl ['port ' ] : '' ;
76+ $ mantisUrl = "{$ parsedUrl ['scheme ' ]}:// {$ parsedUrl ['host ' ]}$ port/ " ;
77+
78+ $ headers = @get_headers ($ mantisUrl );
79+ if (!$ headers || $ headers [0 ] === 'HTTP/1.1 404 Not Found ' ) {
80+ return 'The given URL is unreachable. If this error persists, please check your internet connection. ' ;
81+ }
82+
83+ return null ;
84+ },
85+ );
5786
5887 $ parsedUrl = parse_url ($ mantisUrl );
59-
60- if ($ parsedUrl === false || !isset ($ parsedUrl ['scheme ' ]) || !isset ($ parsedUrl ['host ' ])) {
61- $ this ->error ("The URL you entered is invalid. " );
62-
63- $ this ->askForMantisUrl ();
64- }
65-
6688 $ port = isset ($ parsedUrl ['port ' ]) ? ': ' . $ parsedUrl ['port ' ] : '' ;
67-
6889 $ mantisUrl = "{$ parsedUrl ['scheme ' ]}:// {$ parsedUrl ['host ' ]}$ port/ " ;
6990
70- // Check if something is available on the given URL
71- // If not, we assume that the URL is wrong.
72- $ headers = @get_headers ($ mantisUrl );
73- if (!$ headers || $ headers [0 ] === 'HTTP/1.1 404 Not Found ' ) {
74- $ this ->error (
75- "The given URL is unreachable. If this error persists, please check your internet connection. "
76- );
77-
78- $ this ->askForMantisUrl ();
79- }
80-
8191 $ this ->config ['mantisUrl ' ] = $ mantisUrl ;
8292 }
8393
8494 private function askForMantisToken (): void
8595 {
8696 $ 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 ' );
88-
89- if (empty ($ token )) {
90- $ this ->error ('The token is empty. Please try again. ' );
91-
92- $ this ->askForMantisToken ();
93- }
9497
95- $ this ->config ['mantisToken ' ] = $ token ;
98+ $ this ->config ['mantisToken ' ] = $ this ->password (
99+ label: 'Mantis API Token ' ,
100+ required: true ,
101+ );
96102 }
97103
98104 private function askForGitHubToken (): void
99105 {
100- $ this ->info ("Head over to https://github.com/settings/tokens, create a new personal access token with the `repo` scope. " );
101-
102- $ token = $ this ->secret ("GitHub Token " );
103-
104- if (empty ($ token )) {
105- $ this ->error ('The token is empty. Please try again. ' );
106-
107- $ this ->askForGitHubToken ();
108- }
109-
110- $ this ->config ['githubToken ' ] = $ token ;
106+ $ this ->info ('Head over to https://github.com/settings/tokens, create a new personal access token with the `repo` scope. ' );
107+
108+ $ this ->config ['githubToken ' ] = $ this ->password (
109+ label: 'GitHub Personal Access Token ' ,
110+ required: true ,
111+ validate: fn ($ value ) => !str_starts_with ($ value , 'ghp_ ' ) && str_starts_with ($ value , 'github_pat_ ' )
112+ ? 'The provided value is not a valid GitHub PAT. '
113+ : null ,
114+ );
111115 }
112116
113117 private function askForGitHubRepository (): void
114118 {
115- $ repository = $ this ->ask ('Enter the GitHub repository you want to create issues for (e.g. user/repository) ' );
116-
117- if (empty ($ repository ) || count (explode ('/ ' , $ repository )) !== 2 ) {
118- $ this ->error ("The given repository is invalid. " );
119-
120- $ this ->askForGitHubRepository ();
121- }
122-
123- $ this ->config ['githubRepository ' ] = $ repository ;
119+ $ this ->config ['githubRepository ' ] = $ this ->ask (
120+ label: 'GitHub Repository(e.g. user/repository) ' ,
121+ placeholder: 'E.g. user/repository ' ,
122+ required: true ,
123+ validate: fn ($ value ) => count (explode ('/ ' , $ value )) !== 2
124+ ? 'Invalid repository name. '
125+ : null ,
126+ );
124127 }
125128
126129 private function saveConfig (): void
@@ -135,13 +138,13 @@ private function saveConfig(): void
135138
136139 render (
137140 <<<HTML
138- <div class="my -1 ml-1 px-1 bg-green-500 text-gray-900">
141+ <div class="mb -1 ml-2 px-1 bg-green-500 text-gray-900">
139142 <strong>🚀 Ready for liftoff! 🚀</strong>
140143</div>
141144HTML
142145 );
143146
144- $ this ->success (" Synchronize your first issue by running `mantis2github sync`!\n" );
147+ $ this ->success (' Synchronize your first issue by running `mantis2github sync`! ' );
145148 }
146149
147150 private function readExistingConfigFromPath (): void
0 commit comments