Last active
January 3, 2019 19:36
-
-
Save winni4eva/96f28e234bea5fed301af88362d8f7e1 to your computer and use it in GitHub Desktop.
Fetch user command with progress reporting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
class FetchUsersInfo extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'users:fetch'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Fetch app user information'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$client = new \GuzzleHttp\Client(); | |
$pages = collect([1,2,3,4,5]); | |
$progressBar = $this->output->createProgressBar($pages->count()); | |
$progressBar->start(); | |
$pages->map(function($page) use($client, $progressBar) { | |
$this->output->write("<info> Fetching user info for page => </info>"); | |
$this->output->write("<info>$page</info>"); | |
$res = $client->request('GET', "https://reqres.in/api/users?page=$page"); | |
if($res->getStatusCode() == 200) { | |
logger($res->getBody()); | |
} | |
sleep(30); | |
$progressBar->advance(); | |
}); | |
$progressBar->finish(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment