Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for deleted products in Magento #9

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
os: [ubuntu-latest]
php: [8.1, 8.2]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]
stability: [prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
</p>

This package tracks if products exist in Magento by storing the status locally in the DB.
We developed this to prevent multiple calls when multiple packages need to check product existance in Magento.
This package does do the assumption that once a product exists in Magento it will always be there.
We developed this to prevent multiple calls when multiple packages need to check product existence in Magento.


## Installation
Expand All @@ -26,6 +25,9 @@ $schedule->command(\JustBetter\MagentoProducts\Commands\CheckKnownProductsExiste
$schedule->command(\JustBetter\MagentoProducts\Commands\DiscoverMagentoProductsCommand::class)->daily();
```

> [!IMPORTANT]
> This package requires Job Batching

## Usage

### Checking if a product exists in Magento
Expand Down
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
"laravel/framework": "^10.0"
},
"require-dev": {
"laravel/pint": "^1.6",
"nunomaduro/larastan": "^2.5",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^10.0"
"laravel/pint": "^1.6",
"nunomaduro/larastan": "^2.5",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.0",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^10.0",
"doctrine/dbal": "^3.7.1"
},
"authors": [
{
Expand Down Expand Up @@ -43,7 +46,10 @@
"fix-style": "pint"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('magento_products', function (Blueprint $table): void {
$table->boolean('retrieved')->default(false)->after('last_checked');
});
}

public function down(): void
{
Schema::dropColumns('magento_products', ['retrieved']);
}
};
27 changes: 12 additions & 15 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Tests">
<directory>./tests/*</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Tests">
<directory>./tests/*</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
30 changes: 30 additions & 0 deletions src/Actions/CheckRemovedProducts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace JustBetter\MagentoProducts\Actions;

use JustBetter\MagentoProducts\Contracts\ChecksRemovedProducts;
use JustBetter\MagentoProducts\Events\ProductDeletedInMagentoEvent;
use JustBetter\MagentoProducts\Models\MagentoProduct;

class CheckRemovedProducts implements ChecksRemovedProducts
{
public function check(): void
{
$query = MagentoProduct::query()
->where('exists_in_magento', '=', true)
->where('retrieved', '=', false);

$skus = $query->select(['sku'])->get();

$query->update([
'exists_in_magento' => false,
]);

$skus->each(fn (MagentoProduct $product) => ProductDeletedInMagentoEvent::dispatch($product->sku));
}

public static function bind(): void
{
app()->singleton(ChecksRemovedProducts::class, static::class);
}
}
46 changes: 46 additions & 0 deletions src/Actions/DiscoverMagentoProducts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace JustBetter\MagentoProducts\Actions;

use Illuminate\Bus\Batch;
use JustBetter\MagentoProducts\Contracts\DiscoversMagentoProducts;
use JustBetter\MagentoProducts\Contracts\ProcessesMagentoSkus;
use JustBetter\MagentoProducts\Contracts\RetrievesMagentoSkus;
use JustBetter\MagentoProducts\Jobs\DiscoverMagentoProductsJob;
use JustBetter\MagentoProducts\Models\MagentoProduct;

class DiscoverMagentoProducts implements DiscoversMagentoProducts
{
public function __construct(
protected RetrievesMagentoSkus $magentoSkus,
protected ProcessesMagentoSkus $processor
) {
}

public function discover(int $page, Batch $batch): void
{
if ($page === 0) {
MagentoProduct::query()
->update(['retrieved' => false]);
}

$skus = $this->magentoSkus->retrieve($page);

$hasNextPage = $skus->count() == config('magento-products.page_size');

if ($hasNextPage) {
$batch->add(new DiscoverMagentoProductsJob($page + 1));
}

MagentoProduct::query()
->whereIn('sku', $skus)
->update(['retrieved' => true]);

$this->processor->process($skus);
}

public static function bind(): void
{
app()->singleton(DiscoversMagentoProducts::class, static::class);
}
}
1 change: 1 addition & 0 deletions src/Actions/ProcessMagentoSkus.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function process(Enumerable $skus): void
->whereIn('sku', $skus)
->where('exists_in_magento', true)
->select(['sku'])
->distinct()
->get()
->pluck('sku');

Expand Down
12 changes: 7 additions & 5 deletions src/Commands/DiscoverMagentoProductsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace JustBetter\MagentoProducts\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Bus;
use JustBetter\MagentoProducts\Jobs\CheckRemovedProductsJob;
use JustBetter\MagentoProducts\Jobs\DiscoverMagentoProductsJob;

class DiscoverMagentoProductsCommand extends Command
Expand All @@ -13,11 +15,11 @@ class DiscoverMagentoProductsCommand extends Command

public function handle(): int
{
$this->info('Dispatching...');

DiscoverMagentoProductsJob::dispatch();

$this->info('Done!');
Bus::batch([new DiscoverMagentoProductsJob])
->name('Discover Magento Products')
->then(fn () => CheckRemovedProductsJob::dispatch())
->onQueue(config('magento-products.queue'))
->dispatch();

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

namespace JustBetter\MagentoProducts\Contracts;

interface ChecksRemovedProducts
{
public function check(): void;
}
10 changes: 10 additions & 0 deletions src/Contracts/DiscoversMagentoProducts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace JustBetter\MagentoProducts\Contracts;

use Illuminate\Bus\Batch;

interface DiscoversMagentoProducts
{
public function discover(int $page, Batch $batch): void;
}
2 changes: 1 addition & 1 deletion src/Events/ProductCreatedInMagentoEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace JustBetter\MagentoProducts\Events;

use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Events\Dispatchable;

class ProductCreatedInMagentoEvent
{
Expand Down
14 changes: 14 additions & 0 deletions src/Events/ProductDeletedInMagentoEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace JustBetter\MagentoProducts\Events;

use Illuminate\Foundation\Events\Dispatchable;

class ProductDeletedInMagentoEvent
{
use Dispatchable;

public function __construct(public string $sku)
{
}
}
2 changes: 1 addition & 1 deletion src/Events/ProductDiscoveredEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace JustBetter\MagentoProducts\Events;

use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Events\Dispatchable;

class ProductDiscoveredEvent
{
Expand Down
31 changes: 31 additions & 0 deletions src/Jobs/CheckRemovedProductsJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace JustBetter\MagentoProducts\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\Queue\SerializesModels;
use JustBetter\MagentoProducts\Contracts\ChecksRemovedProducts;

class CheckRemovedProductsJob implements ShouldBeUnique, ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

public int $tries = 1;

public function __construct()
{
$this->onQueue(config('magento-products.queue'));
}

public function handle(ChecksRemovedProducts $contract): void
{
$contract->check();
}
}
23 changes: 6 additions & 17 deletions src/Jobs/DiscoverMagentoProductsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use JustBetter\MagentoProducts\Contracts\ProcessesMagentoSkus;
use JustBetter\MagentoProducts\Contracts\RetrievesMagentoSkus;
use JustBetter\MagentoProducts\Contracts\DiscoversMagentoProducts;

class DiscoverMagentoProductsJob implements ShouldBeUnique, ShouldQueue
{
Expand All @@ -31,23 +30,13 @@ public function __construct(public int $page = 0)
$this->onQueue(config('magento-products.queue'));
}

public function handle(
RetrievesMagentoSkus $retrievesMagentoSkus,
ProcessesMagentoSkus $processesMagentoSkus
): void {
$skus = $retrievesMagentoSkus->retrieve($this->page);

$hasNextPage = $skus->count() == config('magento-products.page_size', 50);

if ($hasNextPage) {
if ($this->batching()) {
$this->batch()->add(new static($this->page + 1)); /** @phpstan-ignore-line */
} else {
static::dispatch($this->page + 1);
}
public function handle(DiscoversMagentoProducts $contract): void
{
if ($this->batch() === null || $this->batch()->cancelled()) {
return;
}

$processesMagentoSkus->process($skus);
$contract->discover($this->page, $this->batch());
}

public function tags(): array
Expand Down
20 changes: 5 additions & 15 deletions src/Models/MagentoProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace JustBetter\MagentoProducts\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use JustBetter\MagentoProducts\Contracts\ChecksMagentoExistence;
use Illuminate\Support\Carbon;

/**
* @property int $id
Expand All @@ -15,6 +14,9 @@
* @property bool $enabled
* @property ?array $data
* @property ?Carbon $last_checked
* @property bool $retrieved
* @property ?Carbon $created_at
* @property ?Carbon $updated_at
*/
class MagentoProduct extends Model
{
Expand All @@ -24,6 +26,7 @@ class MagentoProduct extends Model
'data' => 'array',
'exists_in_magento' => 'boolean',
'last_checked' => 'datetime',
'retrieved' => 'boolean',
];

public static function findBySku(string $sku, string $store = null): ?static
Expand All @@ -36,17 +39,4 @@ public static function findBySku(string $sku, string $store = null): ?static

return $item;
}

/**
* @deprecated Use the action ChecksMagentoExistence instead
*
* @codeCoverageIgnore
*/
public static function existsInMagento(string $sku): bool
{
/** @var ChecksMagentoExistence $action */
$action = app(ChecksMagentoExistence::class);

return $action->exists($sku);
}
}
Loading
Loading