1616use App \Support \Validator ;
1717use Illuminate \Support \Str ;
1818use LaravelZero \Framework \Commands \Command ;
19+ use Symfony \Component \Console \Input \InputInterface ;
20+ use Symfony \Component \Console \Output \OutputInterface ;
1921
2022class PrefixCommand extends Command
2123{
@@ -30,6 +32,7 @@ class PrefixCommand extends Command
3032 {personal-access-token : Personal Access Token generates on https://php-prefixer.com}
3133 {project-id : The project ID to process the source code}
3234 {--github-access-token= : Github access token for private repositories}
35+ {--delete-build : Delete the build after download}
3336 ' ;
3437
3538 /**
@@ -46,24 +49,26 @@ class PrefixCommand extends Command
4649 */
4750 public function handle ()
4851 {
52+ $ start = microtime (true );
53+
4954 $ validator = new Validator ();
50- $ sourceDirectory = realpath ($ this ->argumentOrEnv ('source-directory ' ));
55+ $ sourceDirectory = realpath ($ this ->argument ('source-directory ' ));
5156
5257 if (!$ validator ->isValidSourceDirectory ($ sourceDirectory )) {
5358 $ this ->error ("{$ sourceDirectory } not found " );
5459
5560 return 1 ;
5661 }
5762
58- $ targetDirectory = realpath ($ this ->argumentOrEnv ('target-directory ' ));
63+ $ targetDirectory = realpath ($ this ->argument ('target-directory ' ));
5964
6065 if (!$ validator ->isValidTargetDirectory ($ targetDirectory )) {
6166 $ this ->error ("{$ targetDirectory } not found " );
6267
6368 return 1 ;
6469 }
6570
66- $ personalAccessToken = $ this ->argumentOrEnv ('personal-access-token ' );
71+ $ personalAccessToken = $ this ->argument ('personal-access-token ' );
6772
6873 if (!$ validator ->isPersonalAccessToken ($ personalAccessToken )) {
6974 $ this ->error (
@@ -73,7 +78,7 @@ public function handle()
7378 return 1 ;
7479 }
7580
76- $ projectId = (int ) $ this ->argumentOrEnv ('project-id ' );
81+ $ projectId = (int ) $ this ->argument ('project-id ' );
7782
7883 if (!$ validator ->isValidProjectId ($ personalAccessToken , $ projectId )) {
7984 $ this ->error (
@@ -83,7 +88,7 @@ public function handle()
8388 return 1 ;
8489 }
8590
86- $ githubAccessToken = $ this ->optionOrEnv ('github-access-token ' );
91+ $ githubAccessToken = $ this ->option ('github-access-token ' );
8792
8893 if ($ githubAccessToken && !$ validator ->isValidGithubAccessToken ($ githubAccessToken )) {
8994 $ this ->error (
@@ -93,44 +98,70 @@ public function handle()
9398 return 1 ;
9499 }
95100
101+ $ deleteBuild = $ this ->hasOption ('delete-build ' );
102+
96103 $ processor = new Processor ($ personalAccessToken );
97104 $ build = $ processor ->run ($ sourceDirectory , $ targetDirectory , $ projectId , $ githubAccessToken );
98105
99- switch ($ build ->state ) {
106+ if ($ deleteBuild ) {
107+ $ processor ->deleteBuild ($ projectId , $ build ->id );
108+ }
109+
110+ return $ this ->renderOutput ($ build ->state , $ start );
111+ }
112+
113+ protected function initialize (InputInterface $ input , OutputInterface $ output )
114+ {
115+ parent ::initialize ($ input , $ output );
116+
117+ $ this ->argumentOrEnv ($ input , 'source-directory ' );
118+ $ this ->argumentOrEnv ($ input , 'target-directory ' );
119+ $ this ->argumentOrEnv ($ input , 'personal-access-token ' );
120+ $ this ->argumentOrEnv ($ input , 'project-id ' );
121+ $ this ->optionOrEnv ($ input , 'github-access-token ' );
122+ $ this ->optionOrEnv ($ input , 'delete-build ' );
123+ }
124+
125+ private function argumentOrEnv ($ input , $ key )
126+ {
127+ if (!$ input ->hasArgument ($ key ) || null === $ input ->getArgument ($ key )) {
128+ $ input ->setArgument ($ key , env (Str::upper (Str::snake ($ key ))));
129+ }
130+ }
131+
132+ private function optionOrEnv ($ input , $ key )
133+ {
134+ if (!$ input ->hasOption ($ key ) || null === $ input ->getOption ($ key )) {
135+ $ input ->setOption ($ key , env (Str::upper (Str::snake ($ key ))));
136+ }
137+ }
138+
139+ private function renderOutput ($ state , $ start )
140+ {
141+ $ processingTime = round (microtime (true ) - $ start , 2 );
142+ $ formattedProcessingTime = ' -- Processing time: ' .number_format ($ processingTime ).' seconds ' ;
143+
144+ switch ($ state ) {
100145 case 'success ' :
101146 $ this ->info ('Project prefixed successfully. ' );
147+ $ this ->info ($ formattedProcessingTime );
102148
103149 return 0 ;
104150 case 'cancelled ' :
105151 $ this ->error ('Project prefixing cancelled. ' );
152+ $ this ->info ($ formattedProcessingTime );
106153
107154 return 1 ;
108155 case 'failed ' :
109156 $ this ->error ('Project prefixing failed. ' );
157+ $ this ->info ($ formattedProcessingTime );
110158
111159 return 1 ;
112160 }
113161
114- $ this ->error ('Project prefixing error. ' - $ build ->state );
162+ $ this ->error ('Project prefixing error. ( ' .$ state .') ' );
163+ $ this ->info ($ formattedProcessingTime );
115164
116165 return 1 ;
117166 }
118-
119- private function argumentOrEnv ($ key )
120- {
121- if ($ value = $ this ->argument ($ key )) {
122- return $ value ;
123- }
124-
125- return env ($ key );
126- }
127-
128- private function optionOrEnv ($ key )
129- {
130- if ($ value = $ this ->option ($ key )) {
131- return $ value ;
132- }
133-
134- return env (Str::upper (Str::snake ($ key )));
135- }
136167}
0 commit comments