From 10d4c3c745063d5c46c9ffa52f7873275222d9a4 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Wed, 13 Nov 2024 14:28:17 +0100 Subject: [PATCH 1/4] Improve error logging --- README.md | 1 + src/Actions/Product/SaveProduct.php | 1 + src/Actions/Product/UpdateProduct.php | 1 + src/Akeneo/Types/NumberType.php | 2 +- src/Akeneo/Types/PriceCollectionType.php | 4 +- src/Exceptions/InvalidValueException.php | 5 +- src/Jobs/Product/RetrieveProductJob.php | 16 ++++++ src/Jobs/Product/SaveProductJob.php | 20 +++++++ src/Jobs/Product/UpdateProductJob.php | 3 +- .../ProductModel/RetrieveProductModelJob.php | 16 ++++++ src/Jobs/ProductModel/SaveProductModelJob.php | 16 ++++++ src/Models/Product.php | 18 ++++++ src/Models/ProductModel.php | 11 ++++ tests/Jobs/Product/RetrieveProductJobTest.php | 28 ++++++++++ tests/Jobs/Product/SaveProductJobTest.php | 55 +++++++++++++++++++ .../RetrieveProductModelJobTest.php | 28 ++++++++++ .../ProductModel/SaveProductModelJobTest.php | 54 ++++++++++++++++++ 17 files changed, 272 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3b06872..283ca7a 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,7 @@ Currently, the following attribute types are supported by this package: - `pim_catalog_boolean` - `pim_catalog_price_collection` - `pim_catalog_simpleselect` +- `pim_catalog_multiselect` ## Commands diff --git a/src/Actions/Product/SaveProduct.php b/src/Actions/Product/SaveProduct.php index 19028f9..669f386 100644 --- a/src/Actions/Product/SaveProduct.php +++ b/src/Actions/Product/SaveProduct.php @@ -33,6 +33,7 @@ public function save(ProductData $productData): void $product->update = $product->isDirty(['checksum']); } + $product->resetFailures(); $product->save(); } diff --git a/src/Actions/Product/UpdateProduct.php b/src/Actions/Product/UpdateProduct.php index 3b64b41..edb16ea 100644 --- a/src/Actions/Product/UpdateProduct.php +++ b/src/Actions/Product/UpdateProduct.php @@ -20,6 +20,7 @@ public function update(Product $product): void $product->update = false; $product->fail_count = 0; $product->failed_at = null; + $product->resetFailures(); $product->save(); } diff --git a/src/Akeneo/Types/NumberType.php b/src/Akeneo/Types/NumberType.php index 6cb92a5..e5dc4f3 100644 --- a/src/Akeneo/Types/NumberType.php +++ b/src/Akeneo/Types/NumberType.php @@ -14,7 +14,7 @@ class NumberType extends BaseType public function format(AttributeData $attributeData, mixed $value): float|int { if (! is_numeric($value)) { - throw new InvalidValueException('The given value is not numeric'); + throw new InvalidValueException($attributeData->code(), $value); } return $attributeData['decimals_allowed'] diff --git a/src/Akeneo/Types/PriceCollectionType.php b/src/Akeneo/Types/PriceCollectionType.php index 9bd693a..956fe78 100644 --- a/src/Akeneo/Types/PriceCollectionType.php +++ b/src/Akeneo/Types/PriceCollectionType.php @@ -25,9 +25,7 @@ public function format(AttributeData $attributeData, mixed $value): array } if (! isset($amount) || ! isset($currency)) { - throw new InvalidValueException( - 'Value is not valid for the price collection type. Got value: '.var_export($value, true) - ); + throw new InvalidValueException($attributeData->code(), $value); } return [ diff --git a/src/Exceptions/InvalidValueException.php b/src/Exceptions/InvalidValueException.php index 3d96ede..7de8606 100644 --- a/src/Exceptions/InvalidValueException.php +++ b/src/Exceptions/InvalidValueException.php @@ -6,5 +6,8 @@ class InvalidValueException extends Exception { - // + public function __construct(string $attribute, mixed $value) + { + parent::__construct('The given value "'.var_export($value, true).'" for attribute "'.$attribute.'" is invalid.'); + } } diff --git a/src/Jobs/Product/RetrieveProductJob.php b/src/Jobs/Product/RetrieveProductJob.php index 2572062..64979c0 100644 --- a/src/Jobs/Product/RetrieveProductJob.php +++ b/src/Jobs/Product/RetrieveProductJob.php @@ -8,6 +8,9 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use JustBetter\AkeneoProducts\Contracts\Product\RetrievesProduct; +use JustBetter\AkeneoProducts\Models\Product; +use Spatie\Activitylog\ActivityLogger; +use Throwable; class RetrieveProductJob implements ShouldBeUnique, ShouldQueue { @@ -37,4 +40,17 @@ public function tags(): array $this->identifier, ]; } + + public function failed(Throwable $throwable): void + { + /** @var ?Product $model */ + $model = Product::query()->firstWhere('identifier', '=', $this->identifier); + + $model?->failed(); + + activity() + ->when($model !== null, fn (ActivityLogger $logger): ActivityLogger => $logger->on($model)) + ->useLog('error') + ->log('Failed to retrieve the product: '.$throwable->getMessage()); + } } diff --git a/src/Jobs/Product/SaveProductJob.php b/src/Jobs/Product/SaveProductJob.php index 214478c..14b4960 100644 --- a/src/Jobs/Product/SaveProductJob.php +++ b/src/Jobs/Product/SaveProductJob.php @@ -9,6 +9,9 @@ use Illuminate\Queue\InteractsWithQueue; use JustBetter\AkeneoProducts\Contracts\Product\SavesProduct; use JustBetter\AkeneoProducts\Data\ProductData; +use JustBetter\AkeneoProducts\Models\Product; +use Spatie\Activitylog\ActivityLogger; +use Throwable; class SaveProductJob implements ShouldBeUnique, ShouldQueue { @@ -38,4 +41,21 @@ public function tags(): array $this->productData->identifier(), ]; } + + public function failed(Throwable $throwable): void + { + /** @var ?Product $model */ + $model = Product::query()->firstWhere('identifier', '=', $this->productData->identifier()); + + $model?->failed(); + + activity() + ->when($model !== null, fn (ActivityLogger $logger): ActivityLogger => $logger->on($model)) + ->useLog('error') + ->withProperties([ + 'message' => $throwable->getMessage(), + 'data' => $this->productData->toArray(), + ]) + ->log('Failed to save the product data'); + } } diff --git a/src/Jobs/Product/UpdateProductJob.php b/src/Jobs/Product/UpdateProductJob.php index 9af2cea..decce06 100644 --- a/src/Jobs/Product/UpdateProductJob.php +++ b/src/Jobs/Product/UpdateProductJob.php @@ -50,9 +50,8 @@ public function failed(Throwable $throwable): void ->on($this->product) ->useLog('error') ->withProperties([ - 'message' => $throwable->getMessage(), 'code' => $throwable->getCode(), ]) - ->log('Failed to update product in Akeneo'); + ->log('Failed to update product in Akeneo: ' . $throwable->getMessage()); } } diff --git a/src/Jobs/ProductModel/RetrieveProductModelJob.php b/src/Jobs/ProductModel/RetrieveProductModelJob.php index 7ff5d86..3a552f3 100644 --- a/src/Jobs/ProductModel/RetrieveProductModelJob.php +++ b/src/Jobs/ProductModel/RetrieveProductModelJob.php @@ -8,6 +8,9 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use JustBetter\AkeneoProducts\Contracts\ProductModel\RetrievesProductModel; +use JustBetter\AkeneoProducts\Models\ProductModel; +use Spatie\Activitylog\ActivityLogger; +use Throwable; class RetrieveProductModelJob implements ShouldBeUnique, ShouldQueue { @@ -37,4 +40,17 @@ public function tags(): array $this->code, ]; } + + public function failed(Throwable $throwable): void + { + /** @var ?ProductModel $model */ + $model = ProductModel::query()->firstWhere('code', '=', $this->code); + + $model?->failed(); + + activity() + ->when($model !== null, fn (ActivityLogger $logger): ActivityLogger => $logger->on($model)) + ->useLog('error') + ->log('Failed to retrieve the productmodel: '.$throwable->getMessage()); + } } diff --git a/src/Jobs/ProductModel/SaveProductModelJob.php b/src/Jobs/ProductModel/SaveProductModelJob.php index c40aa16..e6412a8 100644 --- a/src/Jobs/ProductModel/SaveProductModelJob.php +++ b/src/Jobs/ProductModel/SaveProductModelJob.php @@ -9,6 +9,9 @@ use Illuminate\Queue\InteractsWithQueue; use JustBetter\AkeneoProducts\Contracts\ProductModel\SavesProductModel; use JustBetter\AkeneoProducts\Data\ProductModelData; +use JustBetter\AkeneoProducts\Models\ProductModel; +use Spatie\Activitylog\ActivityLogger; +use Throwable; class SaveProductModelJob implements ShouldBeUnique, ShouldQueue { @@ -38,4 +41,17 @@ public function tags(): array $this->productModelData->code(), ]; } + + public function failed(Throwable $throwable): void + { + /** @var ?ProductModel $model */ + $model = ProductModel::query()->firstWhere('code', '=', $this->productModelData->code()); + + $model?->failed(); + + activity() + ->when($model !== null, fn (ActivityLogger $logger): ActivityLogger => $logger->on($model)) + ->useLog('error') + ->log('Failed to save the productmodel: ' . $throwable->getMessage()); + } } diff --git a/src/Models/Product.php b/src/Models/Product.php index 337fca4..afa4081 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Carbon; use JustBetter\AkeneoProducts\Retrievers\Product\BaseProductRetriever; +use Spatie\Activitylog\LogOptions; +use Spatie\Activitylog\Traits\LogsActivity; /** * @property int $id @@ -27,6 +29,7 @@ class Product extends Model { use SoftDeletes; + use LogsActivity; protected $table = 'akeneo_products'; @@ -69,4 +72,19 @@ public function failed(): void $this->save(); } + + public function resetFailures(): void + { + $this->synchronize = true; + $this->fail_count = 0; + $this->failed_at = null; + } + + public function getActivitylogOptions(): LogOptions + { + return LogOptions::defaults() + ->logOnlyDirty() + ->dontSubmitEmptyLogs() + ->logOnly(['data']); + } } diff --git a/src/Models/ProductModel.php b/src/Models/ProductModel.php index c213d60..92f394a 100644 --- a/src/Models/ProductModel.php +++ b/src/Models/ProductModel.php @@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Carbon; use JustBetter\AkeneoProducts\Retrievers\ProductModel\BaseProductModelRetriever; +use Spatie\Activitylog\LogOptions; +use Spatie\Activitylog\Traits\LogsActivity; /** * @property int $id @@ -27,6 +29,7 @@ class ProductModel extends Model { use SoftDeletes; + use LogsActivity; protected $table = 'akeneo_product_models'; @@ -69,4 +72,12 @@ public function failed(): void $this->save(); } + + public function getActivitylogOptions(): LogOptions + { + return LogOptions::defaults() + ->logOnlyDirty() + ->dontSubmitEmptyLogs() + ->logOnly(['data']); + } } diff --git a/tests/Jobs/Product/RetrieveProductJobTest.php b/tests/Jobs/Product/RetrieveProductJobTest.php index c194337..ec4f65a 100644 --- a/tests/Jobs/Product/RetrieveProductJobTest.php +++ b/tests/Jobs/Product/RetrieveProductJobTest.php @@ -2,8 +2,10 @@ namespace JustBetter\AkeneoProducts\Tests\Jobs\Product; +use Exception; use JustBetter\AkeneoProducts\Contracts\Product\RetrievesProduct; use JustBetter\AkeneoProducts\Jobs\Product\RetrieveProductJob; +use JustBetter\AkeneoProducts\Models\Product; use JustBetter\AkeneoProducts\Tests\TestCase; use Mockery\MockInterface; use PHPUnit\Framework\Attributes\Test; @@ -32,4 +34,30 @@ public function it_has_correct_tags_and_unique_id(): void $this->assertEquals(['identifier'], $job->tags()); $this->assertEquals('identifier', $job->uniqueId()); } + + #[Test] + public function it_can_fail(): void + { + /** @var Product $product */ + $product = Product::query()->create([ + 'identifier' => 'identifier', + 'data' => [], + ]); + + $job = new RetrieveProductJob('identifier'); + $job->failed(new Exception); + + $product->refresh(); + + $this->assertNotNull($product->failed_at); + } + + #[Test] + public function it_can_fail_without_product_model(): void + { + $job = new RetrieveProductJob('identifier'); + $job->failed(new Exception); + + $this->assertTrue(true, 'No exception thrown'); + } } diff --git a/tests/Jobs/Product/SaveProductJobTest.php b/tests/Jobs/Product/SaveProductJobTest.php index f38bfbc..1cef69d 100644 --- a/tests/Jobs/Product/SaveProductJobTest.php +++ b/tests/Jobs/Product/SaveProductJobTest.php @@ -2,9 +2,12 @@ namespace JustBetter\AkeneoProducts\Tests\Jobs\Product; +use Exception; use JustBetter\AkeneoProducts\Contracts\Product\SavesProduct; use JustBetter\AkeneoProducts\Data\ProductData; use JustBetter\AkeneoProducts\Jobs\Product\SaveProductJob; +use JustBetter\AkeneoProducts\Jobs\Product\UpdateProductJob; +use JustBetter\AkeneoProducts\Models\Product; use JustBetter\AkeneoProducts\Tests\TestCase; use Mockery\MockInterface; use PHPUnit\Framework\Attributes\Test; @@ -58,4 +61,56 @@ public function it_has_correct_tags_and_unique_id(): void $this->assertEquals(['identifier'], $job->tags()); $this->assertEquals('identifier', $job->uniqueId()); } + + #[Test] + public function it_can_fail(): void + { + /** @var Product $product */ + $product = Product::query()->create([ + 'identifier' => 'identifier', + 'data' => [], + ]); + + $productData = ProductData::of([ + 'identifier' => 'identifier', + 'values' => [ + 'name' => [ + [ + 'locale' => 'nl_NL', + 'scope' => 'ecommerce', + 'data' => 'Ziggy', + ], + ], + ], + ]); + + $job = new SaveProductJob($productData); + $job->failed(new Exception); + + $product->refresh(); + + $this->assertNotNull($product->failed_at); + } + + #[Test] + public function it_can_fail_without_product_model(): void + { + $productData = ProductData::of([ + 'identifier' => 'identifier', + 'values' => [ + 'name' => [ + [ + 'locale' => 'nl_NL', + 'scope' => 'ecommerce', + 'data' => 'Ziggy', + ], + ], + ], + ]); + + $job = new SaveProductJob($productData); + $job->failed(new Exception); + + $this->assertTrue(true, 'No exception thrown'); + } } diff --git a/tests/Jobs/ProductModel/RetrieveProductModelJobTest.php b/tests/Jobs/ProductModel/RetrieveProductModelJobTest.php index 9e58588..75963f0 100644 --- a/tests/Jobs/ProductModel/RetrieveProductModelJobTest.php +++ b/tests/Jobs/ProductModel/RetrieveProductModelJobTest.php @@ -2,8 +2,10 @@ namespace JustBetter\AkeneoProducts\Tests\Jobs\ProductModel; +use Exception; use JustBetter\AkeneoProducts\Contracts\ProductModel\RetrievesProductModel; use JustBetter\AkeneoProducts\Jobs\ProductModel\RetrieveProductModelJob; +use JustBetter\AkeneoProducts\Models\ProductModel; use JustBetter\AkeneoProducts\Tests\TestCase; use Mockery\MockInterface; use PHPUnit\Framework\Attributes\Test; @@ -32,4 +34,30 @@ public function it_has_correct_tags_and_unique_id(): void $this->assertEquals(['code'], $job->tags()); $this->assertEquals('code', $job->uniqueId()); } + + #[Test] + public function it_can_fail(): void + { + /** @var ProductModel $product */ + $product = ProductModel::query()->create([ + 'code' => 'code', + 'data' => [], + ]); + + $job = new RetrieveProductModelJob('code'); + $job->failed(new Exception); + + $product->refresh(); + + $this->assertNotNull($product->failed_at); + } + + #[Test] + public function it_can_fail_without_product_model(): void + { + $job = new RetrieveProductModelJob('code'); + $job->failed(new Exception); + + $this->assertTrue(true, 'No exception thrown'); + } } diff --git a/tests/Jobs/ProductModel/SaveProductModelJobTest.php b/tests/Jobs/ProductModel/SaveProductModelJobTest.php index a462629..de546fe 100644 --- a/tests/Jobs/ProductModel/SaveProductModelJobTest.php +++ b/tests/Jobs/ProductModel/SaveProductModelJobTest.php @@ -2,9 +2,11 @@ namespace JustBetter\AkeneoProducts\Tests\Jobs\ProductModel; +use Exception; use JustBetter\AkeneoProducts\Contracts\ProductModel\SavesProductModel; use JustBetter\AkeneoProducts\Data\ProductModelData; use JustBetter\AkeneoProducts\Jobs\ProductModel\SaveProductModelJob; +use JustBetter\AkeneoProducts\Models\ProductModel; use JustBetter\AkeneoProducts\Tests\TestCase; use Mockery\MockInterface; use PHPUnit\Framework\Attributes\Test; @@ -58,4 +60,56 @@ public function it_has_correct_tags_and_unique_id(): void $this->assertEquals(['code'], $job->tags()); $this->assertEquals('code', $job->uniqueId()); } + + #[Test] + public function it_can_fail(): void + { + /** @var ProductModel $product */ + $product = ProductModel::query()->create([ + 'code' => 'code', + 'data' => [], + ]); + + $productData = ProductModelData::of([ + 'code' => 'code', + 'values' => [ + 'name' => [ + [ + 'locale' => 'nl_NL', + 'scope' => 'ecommerce', + 'data' => 'Ziggy', + ], + ], + ], + ]); + + $job = new SaveProductModelJob($productData); + $job->failed(new Exception); + + $product->refresh(); + + $this->assertNotNull($product->failed_at); + } + + #[Test] + public function it_can_fail_without_product_model(): void + { + $productData = ProductModelData::of([ + 'code' => 'code', + 'values' => [ + 'name' => [ + [ + 'locale' => 'nl_NL', + 'scope' => 'ecommerce', + 'data' => 'Ziggy', + ], + ], + ], + ]); + + $job = new SaveProductModelJob($productData); + $job->failed(new Exception); + + $this->assertTrue(true, 'No exception thrown'); + } } From 5434d53360c6c7acd23650b9e8fc4ecba5e7bf75 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Wed, 13 Nov 2024 14:31:11 +0100 Subject: [PATCH 2/4] SCA & pint --- src/Jobs/Product/RetrieveProductJob.php | 4 +++- src/Jobs/Product/SaveProductJob.php | 4 +++- src/Jobs/ProductModel/RetrieveProductModelJob.php | 4 +++- src/Jobs/ProductModel/SaveProductModelJob.php | 7 +++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Jobs/Product/RetrieveProductJob.php b/src/Jobs/Product/RetrieveProductJob.php index 64979c0..9bf2e58 100644 --- a/src/Jobs/Product/RetrieveProductJob.php +++ b/src/Jobs/Product/RetrieveProductJob.php @@ -49,7 +49,9 @@ public function failed(Throwable $throwable): void $model?->failed(); activity() - ->when($model !== null, fn (ActivityLogger $logger): ActivityLogger => $logger->on($model)) + ->when($model, function (ActivityLogger $logger, Product $product): ActivityLogger { + return $logger->on($product); + }) ->useLog('error') ->log('Failed to retrieve the product: '.$throwable->getMessage()); } diff --git a/src/Jobs/Product/SaveProductJob.php b/src/Jobs/Product/SaveProductJob.php index 14b4960..477c8f7 100644 --- a/src/Jobs/Product/SaveProductJob.php +++ b/src/Jobs/Product/SaveProductJob.php @@ -50,7 +50,9 @@ public function failed(Throwable $throwable): void $model?->failed(); activity() - ->when($model !== null, fn (ActivityLogger $logger): ActivityLogger => $logger->on($model)) + ->when($model, function (ActivityLogger $logger, Product $product): ActivityLogger { + return $logger->on($product); + }) ->useLog('error') ->withProperties([ 'message' => $throwable->getMessage(), diff --git a/src/Jobs/ProductModel/RetrieveProductModelJob.php b/src/Jobs/ProductModel/RetrieveProductModelJob.php index 3a552f3..9edb829 100644 --- a/src/Jobs/ProductModel/RetrieveProductModelJob.php +++ b/src/Jobs/ProductModel/RetrieveProductModelJob.php @@ -49,7 +49,9 @@ public function failed(Throwable $throwable): void $model?->failed(); activity() - ->when($model !== null, fn (ActivityLogger $logger): ActivityLogger => $logger->on($model)) + ->when($model, function (ActivityLogger $logger, ProductModel $productModel): ActivityLogger { + return $logger->on($productModel); + }) ->useLog('error') ->log('Failed to retrieve the productmodel: '.$throwable->getMessage()); } diff --git a/src/Jobs/ProductModel/SaveProductModelJob.php b/src/Jobs/ProductModel/SaveProductModelJob.php index e6412a8..941ad45 100644 --- a/src/Jobs/ProductModel/SaveProductModelJob.php +++ b/src/Jobs/ProductModel/SaveProductModelJob.php @@ -10,6 +10,7 @@ use JustBetter\AkeneoProducts\Contracts\ProductModel\SavesProductModel; use JustBetter\AkeneoProducts\Data\ProductModelData; use JustBetter\AkeneoProducts\Models\ProductModel; +use JustBetter\MagentoPrices\Models\Price; use Spatie\Activitylog\ActivityLogger; use Throwable; @@ -50,8 +51,10 @@ public function failed(Throwable $throwable): void $model?->failed(); activity() - ->when($model !== null, fn (ActivityLogger $logger): ActivityLogger => $logger->on($model)) + ->when($model, function (ActivityLogger $logger, ProductModel $productModel): ActivityLogger { + return $logger->on($productModel); + }) ->useLog('error') - ->log('Failed to save the productmodel: ' . $throwable->getMessage()); + ->log('Failed to save the productmodel: '.$throwable->getMessage()); } } From 29365066c0bcdefbd64241522e52b0c1503abcc2 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Wed, 13 Nov 2024 14:56:59 +0100 Subject: [PATCH 3/4] Feedback --- src/Actions/ProductModel/SaveProductModel.php | 1 + src/Actions/ProductModel/UpdateProductModel.php | 3 +-- src/Jobs/Product/SaveProductJob.php | 3 +-- src/Jobs/Product/UpdateProductJob.php | 2 +- src/Jobs/ProductModel/SaveProductModelJob.php | 1 - src/Models/Product.php | 2 +- src/Models/ProductModel.php | 9 ++++++++- tests/Jobs/Product/SaveProductJobTest.php | 1 - tests/Jobs/ProductModel/RetrieveProductModelJobTest.php | 8 ++++---- 9 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Actions/ProductModel/SaveProductModel.php b/src/Actions/ProductModel/SaveProductModel.php index efda575..a1f8140 100644 --- a/src/Actions/ProductModel/SaveProductModel.php +++ b/src/Actions/ProductModel/SaveProductModel.php @@ -33,6 +33,7 @@ public function save(ProductModelData $productModelData): void $productModel->update = $productModel->isDirty(['checksum']); } + $productModel->resetFailures(); $productModel->save(); } diff --git a/src/Actions/ProductModel/UpdateProductModel.php b/src/Actions/ProductModel/UpdateProductModel.php index f37c6b4..95e83b6 100644 --- a/src/Actions/ProductModel/UpdateProductModel.php +++ b/src/Actions/ProductModel/UpdateProductModel.php @@ -22,8 +22,7 @@ public function update(ProductModel $productModel): void $productModel->modified_at = now(); $productModel->update = false; - $productModel->fail_count = 0; - $productModel->failed_at = null; + $productModel->resetFailures(); $productModel->save(); } diff --git a/src/Jobs/Product/SaveProductJob.php b/src/Jobs/Product/SaveProductJob.php index 477c8f7..0327bc8 100644 --- a/src/Jobs/Product/SaveProductJob.php +++ b/src/Jobs/Product/SaveProductJob.php @@ -55,9 +55,8 @@ public function failed(Throwable $throwable): void }) ->useLog('error') ->withProperties([ - 'message' => $throwable->getMessage(), 'data' => $this->productData->toArray(), ]) - ->log('Failed to save the product data'); + ->log('Failed to save the product data: '.$throwable->getMessage()); } } diff --git a/src/Jobs/Product/UpdateProductJob.php b/src/Jobs/Product/UpdateProductJob.php index decce06..99685be 100644 --- a/src/Jobs/Product/UpdateProductJob.php +++ b/src/Jobs/Product/UpdateProductJob.php @@ -52,6 +52,6 @@ public function failed(Throwable $throwable): void ->withProperties([ 'code' => $throwable->getCode(), ]) - ->log('Failed to update product in Akeneo: ' . $throwable->getMessage()); + ->log('Failed to update product in Akeneo: '.$throwable->getMessage()); } } diff --git a/src/Jobs/ProductModel/SaveProductModelJob.php b/src/Jobs/ProductModel/SaveProductModelJob.php index 941ad45..43fd373 100644 --- a/src/Jobs/ProductModel/SaveProductModelJob.php +++ b/src/Jobs/ProductModel/SaveProductModelJob.php @@ -10,7 +10,6 @@ use JustBetter\AkeneoProducts\Contracts\ProductModel\SavesProductModel; use JustBetter\AkeneoProducts\Data\ProductModelData; use JustBetter\AkeneoProducts\Models\ProductModel; -use JustBetter\MagentoPrices\Models\Price; use Spatie\Activitylog\ActivityLogger; use Throwable; diff --git a/src/Models/Product.php b/src/Models/Product.php index afa4081..6d16b7b 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -28,8 +28,8 @@ */ class Product extends Model { - use SoftDeletes; use LogsActivity; + use SoftDeletes; protected $table = 'akeneo_products'; diff --git a/src/Models/ProductModel.php b/src/Models/ProductModel.php index 92f394a..551563c 100644 --- a/src/Models/ProductModel.php +++ b/src/Models/ProductModel.php @@ -28,8 +28,8 @@ */ class ProductModel extends Model { - use SoftDeletes; use LogsActivity; + use SoftDeletes; protected $table = 'akeneo_product_models'; @@ -73,6 +73,13 @@ public function failed(): void $this->save(); } + public function resetFailures(): void + { + $this->synchronize = true; + $this->fail_count = 0; + $this->failed_at = null; + } + public function getActivitylogOptions(): LogOptions { return LogOptions::defaults() diff --git a/tests/Jobs/Product/SaveProductJobTest.php b/tests/Jobs/Product/SaveProductJobTest.php index 1cef69d..545b5f3 100644 --- a/tests/Jobs/Product/SaveProductJobTest.php +++ b/tests/Jobs/Product/SaveProductJobTest.php @@ -6,7 +6,6 @@ use JustBetter\AkeneoProducts\Contracts\Product\SavesProduct; use JustBetter\AkeneoProducts\Data\ProductData; use JustBetter\AkeneoProducts\Jobs\Product\SaveProductJob; -use JustBetter\AkeneoProducts\Jobs\Product\UpdateProductJob; use JustBetter\AkeneoProducts\Models\Product; use JustBetter\AkeneoProducts\Tests\TestCase; use Mockery\MockInterface; diff --git a/tests/Jobs/ProductModel/RetrieveProductModelJobTest.php b/tests/Jobs/ProductModel/RetrieveProductModelJobTest.php index 75963f0..8f9cdab 100644 --- a/tests/Jobs/ProductModel/RetrieveProductModelJobTest.php +++ b/tests/Jobs/ProductModel/RetrieveProductModelJobTest.php @@ -38,8 +38,8 @@ public function it_has_correct_tags_and_unique_id(): void #[Test] public function it_can_fail(): void { - /** @var ProductModel $product */ - $product = ProductModel::query()->create([ + /** @var ProductModel $productModel */ + $productModel = ProductModel::query()->create([ 'code' => 'code', 'data' => [], ]); @@ -47,9 +47,9 @@ public function it_can_fail(): void $job = new RetrieveProductModelJob('code'); $job->failed(new Exception); - $product->refresh(); + $productModel->refresh(); - $this->assertNotNull($product->failed_at); + $this->assertNotNull($productModel->failed_at); } #[Test] From 73cc9c8430853b02e88c22e16e540ef8e2fc49f4 Mon Sep 17 00:00:00 2001 From: Vincent Boon Date: Wed, 13 Nov 2024 14:57:29 +0100 Subject: [PATCH 4/4] Feedback --- tests/Jobs/ProductModel/SaveProductModelJobTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Jobs/ProductModel/SaveProductModelJobTest.php b/tests/Jobs/ProductModel/SaveProductModelJobTest.php index de546fe..840c2cd 100644 --- a/tests/Jobs/ProductModel/SaveProductModelJobTest.php +++ b/tests/Jobs/ProductModel/SaveProductModelJobTest.php @@ -70,7 +70,7 @@ public function it_can_fail(): void 'data' => [], ]); - $productData = ProductModelData::of([ + $productModelData = ProductModelData::of([ 'code' => 'code', 'values' => [ 'name' => [ @@ -83,7 +83,7 @@ public function it_can_fail(): void ], ]); - $job = new SaveProductModelJob($productData); + $job = new SaveProductModelJob($productModelData); $job->failed(new Exception); $product->refresh(); @@ -94,7 +94,7 @@ public function it_can_fail(): void #[Test] public function it_can_fail_without_product_model(): void { - $productData = ProductModelData::of([ + $productModelData = ProductModelData::of([ 'code' => 'code', 'values' => [ 'name' => [ @@ -107,7 +107,7 @@ public function it_can_fail_without_product_model(): void ], ]); - $job = new SaveProductModelJob($productData); + $job = new SaveProductModelJob($productModelData); $job->failed(new Exception); $this->assertTrue(true, 'No exception thrown');