コード例 #1
0
 /**
  * Execute the console command.
  *
  * @param Client $client
  *
  * @return mixed
  */
 public function handle(Client $client)
 {
     // Basic setup for offset-building
     $maxNumPackages = 5;
     $totalNumPackages = Package::count();
     $numResultSets = ceil($totalNumPackages / $maxNumPackages);
     // Paginate through results
     for ($numResultSet = 0; $numResultSet < $numResultSets; $numResultSet++) {
         // Get current result set
         $packages = Package::skip($numResultSet * $maxNumPackages)->take($maxNumPackages)->get();
         // Go through current result set
         foreach ($packages as $package) {
             $this->info("Fetching package information for {$package->name}...");
             // Get package info
             $packageInfo = $this->getPackageInfo($package->name, $client);
             // Validate
             if ($packageInfo === false) {
                 $this->warn('Package skipped.');
                 continue;
             }
             // Check if it is a GitHub repository
             if (!$this->isGitHubRepository($packageInfo->package->repository)) {
                 $this->warn("Package {$packageName} is NOT a GitHub repository and will be skipped!");
                 continue;
             }
             // Get GitHub package name
             $packageName = $this->getPackageNameFromUrl($packageInfo->package->repository);
             $this->info("Fetching contributors for {$packageName}...");
             // Get contributors on GitHub of this repository
             $contributors = $this->getContributors($packageName, $client);
             // Validate
             if ($contributors === false) {
                 $this->warn('Package skipped.');
                 continue;
             }
             // Go through contributors
             foreach ($contributors as $contributor) {
                 // Find or create contributor
                 $contributor = Contributor::firstOrCreate(['name' => $contributor]);
                 // Attach contributor to current package if it isn't yet
                 if (!$package->contributors->contains($contributor->contributor_id)) {
                     $package->contributors()->attach($contributor->contributor_id);
                 }
             }
             $this->info('OK');
         }
     }
 }