Skip to content

Instantly share code, notes, and snippets.

@dmaksimov
Last active February 16, 2022 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmaksimov/fe5371742c8bc7a7a4803679dab2ca80 to your computer and use it in GitHub Desktop.
Save dmaksimov/fe5371742c8bc7a7a4803679dab2ca80 to your computer and use it in GitHub Desktop.
<?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;
}
}
@phuclh
Copy link

phuclh commented Feb 16, 2022

/**
     * 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(trim($value), $result[$name])) {
                    $result[$name][] = trim($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(trim($value->name), $result[$productAttribute->name])) {
                    $result[$productAttribute->name][] = trim($value->name);
                }
            }
        }

        return $result;
    }

This will remove white space before do checking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment