Last active
February 16, 2022 22:08
-
-
Save dmaksimov/fe5371742c8bc7a7a4803679dab2ca80 to your computer and use it in GitHub Desktop.
This file contains 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 App\Models\LocallyProduct; | |
use Illuminate\Console\Command; | |
class TestLocallySync extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'test-locally-sync'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Command description'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
$results = (object) [ | |
'matches' => 0, | |
'no_matches' => 0, | |
]; | |
$allLocallyProducts = LocallyProduct::where('product_id', '!=', '')->get(); | |
$locallyProductGroups = $allLocallyProducts->groupBy('handle'); | |
foreach ($locallyProductGroups as $handle => $locallyProducts) { | |
$options = $this->getLocallyOptions($locallyProducts); | |
$product = $locallyProducts->first()->product; | |
$productAttributes = $this->getProductAttributes($product); | |
if (array_same_recursive($options, $productAttributes)) { | |
$this->info('#' . $product->id . ' matches'); | |
$results->matches++; | |
} else { | |
$this->warn('#' . $product->id . ' no match'); | |
$results->no_matches++; | |
$this->line('Locally options:'); | |
dump($options); | |
$this->line('Product attributes:'); | |
dump($productAttributes); | |
$this->line(''); | |
} | |
} | |
$this->line(''); | |
$this->info('Results:'); | |
dump($results); | |
return 0; | |
} | |
protected function getLocallyOptions($locallyProducts) | |
{ | |
$result = []; | |
foreach ($locallyProducts as $locallyProduct) { | |
foreach ($locallyProduct->options as $name => $value) { | |
if (empty($name) || empty($value)) { | |
continue; | |
} | |
if (! array_key_exists($name, $result)) { | |
$result[$name] = []; | |
} | |
if (! in_array($value, $result[$name])) { | |
$result[$name][] = $value; | |
} | |
} | |
} | |
return $result; | |
} | |
protected function getProductAttributes($product) | |
{ | |
$result = []; | |
foreach ($product->productAttributes as $productAttribute) { | |
if (! array_key_exists($productAttribute->name, $result)) { | |
$result[$productAttribute->name] = []; | |
} | |
foreach ($productAttribute->values as $value) { | |
if (! in_array($value->name, $result[$productAttribute->name])) { | |
$result[$productAttribute->name][] = $value->name; | |
} | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will remove white space before do checking