/**
  * Execute the console command.
  *
  * @param Settings $settings
  * @return mixed
  */
 public function handle()
 {
     $client = new \Github\Client();
     if ($this->cache && config('game-of-tests.cache') && class_exists(CacheItemPool::class)) {
         $client->addCache(new CacheItemPool($this->cache));
     }
     $this->info('Getting repository list');
     if ($this->argument('organisation') !== false) {
         $repositories = $client->organization()->repositories($this->argument('organisation'), 'owner');
     }
     $repositoryUrls = array_pluck($repositories, 'clone_url');
     $this->info('Found ' . count($repositoryUrls) . ' repositories on Github');
     $progresbar = $this->output->createProgressBar(count($repositoryUrls));
     $progresbar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %message% ');
     $progresbar->setMessage('Searching...');
     \Swis\GotLaravel\Models\Results::unguard();
     $inspector = new Inspector($this->settings);
     foreach ($repositoryUrls as $gitUrl) {
         $repository = $inspector->getRepositoryByUrl($gitUrl);
         // Check modified date
         if (null !== $this->option('modified')) {
             $modifiedTimestamp = strtotime($this->option('modified'));
             /**
              * @var $date \DateTime
              */
             try {
                 $commitDate = $repository->getHeadCommit()->getCommitterDate();
             } catch (\Gitonomy\Git\Exception\ReferenceNotFoundException $e) {
                 $this->error($e->getMessage());
                 $this->error('Error finding reference for ' . $gitUrl);
             }
             if ($modifiedTimestamp === false || $commitDate->getTimestamp() < $modifiedTimestamp) {
                 $progresbar->advance();
                 continue;
             }
         }
         $repository->setLogger(new ConsoleLogger($this->getOutput()));
         $resultSet = $inspector->inspectRepository($repository);
         $remote = $resultSet['remote'];
         if (count($resultSet['results']) > 0) {
             $progresbar->setMessage('Found ' . count($resultSet['results']) . ' tests for ' . $remote);
         }
         if (!$this->option('dry-run')) {
             foreach ($resultSet['results'] as $result) {
                 $insert = $result->toArray();
                 $insert['remote'] = $remote;
                 $insert['author_slug'] = Str::slug($result->getAuthor());
                 $insert['created_at'] = Carbon::createFromTimestamp($insert['date']);
                 try {
                     \Swis\GotLaravel\Models\Results::updateOrCreate(array_only($insert, ['remote', 'filename', 'line']), $insert);
                 } catch (\Exception $e) {
                     $this->error('Couldnt insert: ' . $e->getMessage() . PHP_EOL . print_r($insert, 1));
                 }
             }
         }
         $progresbar->advance();
     }
     Artisan::call('got:normalize-names');
 }