예제 #1
0
파일: Import.php 프로젝트: tdt/input
 /**
  * Execute the console command.
  *
  * @return void
  */
 public function fire()
 {
     // Get the input file from the arguments, if none specified use the default file path from the export command
     $file = $this->argument('file');
     if (empty($file)) {
         $file = Export::getExportFile();
     }
     $safe = $this->option('safe');
     // Get the contents from the file if it exists
     if (\File::exists($file)) {
         $content = json_decode(\File::get($file), true);
         // If the content is legit, proceed to make the calls to the input endpoint
         if ($content) {
             foreach ($content as $identifier => $job_definition) {
                 // If the safe option is passed, prompt the user with the identifier and body
                 if ($safe) {
                     if (!$this->confirm("A job with identifier {$identifier} is about to be added, are you sure about this? [y|n]")) {
                         $this->info("The job with identifier {$identifier} was prevented of being added.");
                         break;
                     }
                 }
                 $this->updateRequest('PUT', array(), $job_definition);
                 $input_controller = new InputController();
                 $response = $input_controller->createJob($identifier);
                 $status_code = $response->getStatusCode();
                 if ($status_code == 200) {
                     $this->info("A new definition with identifier ({$identifier}) was succesfully added.");
                 } else {
                     $this->error("A status of {$status_code} was returned when adding {$identifier}, check the logs for indications of what may have gone wrong.");
                 }
             }
         } else {
             $this->error("We failed to extract the input jobs from the json, make sure the JSON content is valid.");
             die;
         }
     } else {
         $this->error("We couldn't find the file ({$file}) containing the input jobs, make sure the path is reachable for the datatank.");
         die;
     }
 }