Skip to content

Commit

Permalink
Merge pull request #16 from kevinmeijer97/master
Browse files Browse the repository at this point in the history
Refactored Product import and added separate delete job
  • Loading branch information
pimruiter authored Jan 27, 2023
2 parents 68cb5cd + bb095e7 commit 0f3f6ff
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/Actions/Categories/CreateCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function create(Collection $categories, string $storeCode): \Illuminate\S
'magento_parent_id' => $category->parent_id ?? '',
'title' => $category->name,
'slug' => Str::replace('/', '-', $category->url_path),
'url_path' => $category->url_path ?? '',
'url_path' => $category->url_path ? '/' . $category->url_path : '',
])->each(fn ($category) => Entry::updateOrCreate($category, 'categories', 'slug', $storeCode));
}
}
14 changes: 5 additions & 9 deletions src/Actions/Products/CreateProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

namespace Rapidez\Statamic\Actions\Products;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Enumerable;
use Statamic\Facades\Entry;

class CreateProducts
{
public function create(Collection $products, string $storeCode): \Illuminate\Support\Collection
public function create(Enumerable $products, string $storeCode): Enumerable
{
if ($products->isEmpty()) {
return new Collection();
}

return $products->map(fn ($product) => [
return $products->map(fn ($product): array => [
'sku' => $product->sku,
'title' => $product->name
])->each(fn ($product) => Entry::updateOrCreate($product, 'products', 'sku', $storeCode));
'title' => $product->name,
])->each(fn (array $product) => Entry::updateOrCreate($product, 'products', 'sku', $storeCode));
}
}
85 changes: 69 additions & 16 deletions src/Actions/Products/ImportProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,103 @@

namespace Rapidez\Statamic\Actions\Products;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Enumerable;
use Illuminate\Support\Facades\DB;
use Rapidez\Core\Models\Model;
use Rapidez\Statamic\Events\ProductsImportedEvent;
use Rapidez\Statamic\Jobs\CreateProductsJob;
use Statamic\Facades\Site;

class ImportProducts
{
public function __construct(
public CreateProducts $createProducts,
public DeleteProducts $deleteProducts,
protected int $chunkSize = 200
protected int $chunkSize = 500
) {
}

public function import(
?string $store = null
): void
public function import(?Carbon $updatedAt = null, ?string $store = null): void
{
$productModel = config('rapidez.models.product');

foreach(Site::all() as $site) {
$sites = Site::all();

if ($store !== null) {
$sites = collect([$store => Site::get($store)]);
}

$sites->each(function ($site) use ($productModel, $updatedAt) {
$siteAttributes = $site->attributes();

if (!isset($siteAttributes['magento_store_id'])) {
continue;
return;
}

config()->set('rapidez.store', $siteAttributes['magento_store_id']);

$productSkus = collect();
$childIds = DB::table('catalog_product_super_link')->select(['product_id'])->get()->pluck('product_id');
$flat = (new $productModel())->getTable();
$childIds = DB::table('catalog_product_super_link')->pluck('product_id');

/** @var Model $flat */
$flat = new $productModel();

$productQuery = $productModel::selectOnlyIndexable()
->where($flat . '.type_id', 'configurable')
->orWhereNotIn($flat . '.entity_id', $childIds->toArray())
->when($updatedAt !== null, function (Builder $builder) use ($updatedAt): void {
$builder->where($builder->qualifyColumn('updated_at'), '>=', $updatedAt);
})
->where(function (Builder $builder) use ($childIds): void {
$builder
->where($builder->qualifyColumn('type_id'), 'configurable')
->orWhereNotIn($builder->qualifyColumn('entity_id'), $childIds->toArray());
})
->withEventyGlobalScopes('statamic.product.scopes');

$productQuery->chunk($this->chunkSize, function ($products) use ($site, &$productSkus) {
$products = $this->createProducts->create($products, $site->handle());
$productSkus = $productSkus->merge($products->map(fn ($product) => $product['sku']));
$productQuery->chunk($this->chunkSize, function (Enumerable $products) use ($site): void {
CreateProductsJob::dispatch($products, $site->handle());
});
});

$this->deleteProducts->deleteOldProducts($productSkus, $site->handle());
ProductsImportedEvent::dispatch();
}

public function delete(?string $store = null): void
{
$productModel = config('rapidez.models.product');

$sites = Site::all();

if ($store !== null) {
$sites = collect([$store => Site::get($store)]);
}

ProductsImportedEvent::dispatch();
$sites->each(function ($site) use ($productModel) {
$siteAttributes = $site->attributes();

if (!isset($siteAttributes['magento_store_id'])) {
return;
}

config()->set('rapidez.store', $siteAttributes['magento_store_id']);

$childIds = DB::table('catalog_product_super_link')->pluck('product_id');

/** @var Model $flat */
$flat = new $productModel();

$productSkus = collect();

$productQuery = $productModel::selectOnlyIndexable()
->where($flat->qualifyColumn('type_id'), 'configurable')
->orWhereNotIn($flat->qualifyColumn('entity_id'), $childIds->toArray())
->withEventyGlobalScopes('statamic.product.scopes');

$productQuery->chunk($this->chunkSize, function (Enumerable $products) use ($site, &$productSkus): void {
$productSkus = $productSkus->merge($products->map(fn($product) => $product['sku']));
});

$this->deleteProducts->deleteOldProducts($productSkus, $site->handle());
});
}
}
24 changes: 24 additions & 0 deletions src/Commands/DeleteProductsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rapidez\Statamic\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Rapidez\Statamic\Jobs\DeleteProductsJob;

class DeleteProductsCommand extends Command
{
protected $signature = 'rapidez:statamic:delete:products {store?}';

protected $description = 'Delete products that only exist in Statamic.';

public function handle(): int
{
/** @var ?string $store */
$store = $this->argument('store');

DeleteProductsJob::dispatch($store);

return static::SUCCESS;
}
}
15 changes: 13 additions & 2 deletions src/Commands/SyncProductsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@
namespace Rapidez\Statamic\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Rapidez\Statamic\Jobs\ImportProductsJob;

class SyncProductsCommand extends Command
{
protected $signature = 'rapidez:statamic:sync:products {store?}';
protected $signature = 'rapidez:statamic:sync:products {store?} {--updated-at=}';

protected $description = 'Sync the Magento products to Statamic.';

public function handle(): int
{
ImportProductsJob::dispatch($this->argument('store'));
/** @var ?string $store */
$store = $this->argument('store');

/** @var ?string $updatedAt */
$updatedAt = $this->option('updated-at');

if($updatedAt !== null) {
$updatedAt = Carbon::parse($updatedAt);
}

ImportProductsJob::dispatch($updatedAt, $store);

return static::SUCCESS;
}
Expand Down
30 changes: 30 additions & 0 deletions src/Http/Controllers/StatamicRewriteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rapidez\Statamic\Http\Controllers;

use Illuminate\Http\Request;
use Statamic\Facades\Entry;
use Illuminate\Support\Facades\View;
use Statamic\Exceptions\NotFoundHttpException;

class StatamicRewriteController
{
public function __invoke(Request $request)
{
$storeCode = config()->get('rapidez.store_code');

$entry = Entry::query()
->where('collection', 'pages')
->where('locale', $storeCode)
->where('slug', $request->path() == '/' ? 'home' : $request->path())
->first();

if (!$entry) {
throw new NotFoundHttpException();
}

$template = View::exists($entry->template()) ? $entry->template() : $entry->blueprint->handle();

echo view($template, ['entry' => $entry]);
}
}
1 change: 1 addition & 0 deletions src/Http/ViewComposers/StatamicDataComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function compose(View $view) : View
if(!isset($view->globals)) {
$view->with('globals', (object)$this->globals);
}

return $view;
}
}
35 changes: 35 additions & 0 deletions src/Jobs/CreateProductsJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rapidez\Statamic\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Carbon;
use Illuminate\Support\Enumerable;
use Rapidez\Statamic\Actions\Products\CreateProducts;
use Rapidez\Statamic\Actions\Products\ImportProducts;

class CreateProductsJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;

public function __construct(
public Enumerable $products,
public ?string $siteHandle = null,
){
}

public function handle(CreateProducts $createProducts): void
{
if (!$this->products || !$this->siteHandle) {
return;
}

$createProducts->create($this->products, $this->siteHandle);
}
}
28 changes: 28 additions & 0 deletions src/Jobs/DeleteProductsJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rapidez\Statamic\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Carbon;
use Rapidez\Statamic\Actions\Products\ImportProducts;

class DeleteProductsJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;

public function __construct(
public ?string $store = null,
){
}

public function handle(ImportProducts $importProducts): void
{
$importProducts->delete($this->store);
}
}
6 changes: 4 additions & 2 deletions src/Jobs/ImportProductsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Carbon;
use Rapidez\Statamic\Actions\Products\ImportProducts;

class ImportProductsJob implements ShouldQueue, ShouldBeUnique
Expand All @@ -16,12 +17,13 @@ class ImportProductsJob implements ShouldQueue, ShouldBeUnique
use Queueable;

public function __construct(
public ?string $store = null
public ?Carbon $updatedAt = null,
public ?string $store = null,
){
}

public function handle(ImportProducts $importProducts): void
{
$importProducts->import($this->store);
$importProducts->import($this->updatedAt, $this->store);
}
}
6 changes: 5 additions & 1 deletion src/RapidezStatamicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Rapidez\Statamic;

use Illuminate\Support\ServiceProvider;
use Rapidez\Statamic\Http\Controllers\StatamicRewriteController;
use Statamic\Http\Controllers\FrontendController;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Rapidez\Statamic\Commands\DeleteProductsCommand;
use Rapidez\Statamic\Commands\SyncProductsCommand;
use Rapidez\Statamic\Commands\SyncCategoriesCommand;
use Statamic\Facades\Entry;
Expand All @@ -20,6 +22,7 @@
use Validator;
use Statamic\Events\GlobalSetSaved;
use Statamic\Events\GlobalSetDeleted;
use TorMorten\Eventy\Facades\Eventy;

class RapidezStatamicServiceProvider extends ServiceProvider
{
Expand All @@ -39,7 +42,7 @@ public function boot()
public function bootRoutes() : self
{
if (!$this->app->routesAreCached()) {
Rapidez::addFallbackRoute([FrontendController::class, 'index'], 100);
Rapidez::addFallbackRoute(StatamicRewriteController::class, 100);
}

return $this;
Expand All @@ -56,6 +59,7 @@ public function bootCommands() : self
{
$this->commands([
SyncProductsCommand::class,
DeleteProductsCommand::class,
SyncCategoriesCommand::class
]);

Expand Down

0 comments on commit 0f3f6ff

Please sign in to comment.