Esempio n. 1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $page = 1;
     $per_page = 50;
     $notLastPage = true;
     $options = array('page' => $page, 'per_page' => $per_page, 'productfeed' => 'true');
     $this->info('Start importing programs into the database');
     while ($notLastPage) {
         $APIdata = DaisyconHelper::getRestAPI("programs", $options);
         if (is_array($APIdata)) {
             $resultCount = count($APIdata['response']);
             if ($resultCount > 0) {
                 if ($page == 1) {
                     $this->info('Clear database table');
                     Program::truncate();
                 }
                 foreach ($APIdata['response'] as $program) {
                     $program = (array) $program;
                     $program['program_id'] = $program['id'];
                     $program['description'] = $program['descriptions'][0]->description;
                     $program['url'] = DaisyconHelper::changeProgramURL($program['url']);
                     Program::create((array) $program);
                 }
                 $totalCount = Program::all()->count();
                 $comment = sprintf('Page %d loaded with %d record(s); Total records: %d', $page, $resultCount, $totalCount);
                 $this->comment($comment);
             } else {
                 $this->comment('No programs found');
             }
         }
         if ($resultCount < $per_page) {
             $notLastPage = false;
         }
         $options['page'] = $page++;
     }
     $count = Program::all()->count();
     return $this->info($count . ' programs imported');
 }