File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Kiyoh/ArtisanCommands/ImportCommand.php
<?php
namespace App\Komma\Kiyoh\ArtisanCommands;
use App\Komma\Kiyoh\Client;
use App\Komma\Kiyoh\Models\Review;
use App\Komma\Locations\Models\Location;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
class ImportCommand extends Command
{
/** @var string */
/**
* First time:
* php artisan kiyoh:import --limit=1000 --city=Geel
*
* After that:
* php artisan kiyoh:import --city=Geel
*/
protected $signature = 'kiyoh:import
{--city="[city name]" : The name of the city of reviews to fetch}
{--limit=10 : The maximum number of reviews to fetch}
{--with-migrated : Include migrated reviews in the feed}';
/** @var string */
protected $description = 'Import KiyOh reviews into the database';
/** @var \App\Komma\Kiyoh\Feed */
protected $feed;
/** @var \App\Komma\Kiyoh\Client */
protected $client;
public function __construct(Client $client)
{
$this->client = $client;
$this->feed = $client->feed;
parent::__construct();
}
public function handle(): int
{
$location = Location::where('city', '=', $this->option('city'))->first();
if (empty($location)) {
if ($this->option('city') === '"[city name]"') {
$this->error('You have to enter a city name via the --city option.');
} else {
$this->error('City "'.$this->option('city').'" not found!');
}
return 0;
}
$this->client->setApiKey(config('kiyoh.secret_'.strtolower($this->option('city'))));
$this->feed->withMigrated($this->option('with-migrated'))->limit($this->option('limit'));
tap($this->feed->get()->reviews, function ($reviews) use ($location) {
$this->line('Importing KiyOh reviews');
$this->output->newLine();
$this->output->progressStart($reviews->count());
$reviews->sortBy('created_at')->each(function ($review) use ($location) {
Review::updateOrCreate([
'review_id' => $review->uuid,
], [
'location_id' => $location->id,
'rating' => $review->rating,
'recommendation' => $review->recommendation,
'payload' => $this->preparePayload($review),
'created_at' => Carbon::parse($review->created_at),
'updated_at' => Carbon::parse($review->updated_at),
]);
$this->output->progressAdvance();
});
$this->output->progressFinish();
});
return 0;
}
/**
* Prepare the review payload.
*
* @param \App\Komma\Kiyoh\Resources\Review $review
* @return array
*/
protected function preparePayload($review)
{
return collect($review->toArray())->forget([
'uuid',
'rating',
'recommendation',
'created_at',
'updated_at',
]);
}
}