Skip to content
This repository was archived by the owner on Oct 27, 2022. It is now read-only.

Commit c0eaee8

Browse files
authored
Merge pull request #21 from grayloon/remove-old-links
Remove Old Links; Cleanup
2 parents 2589a83 + 0b40da5 commit c0eaee8

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

src/Jobs/WaitForLinkedProductSku.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Grayloon\MagentoStorage\Support\MagentoProducts;
99
use Illuminate\Bus\Queueable;
1010
use Illuminate\Contracts\Queue\ShouldQueue;
11+
use Illuminate\Database\Eloquent\ModelNotFoundException;
1112
use Illuminate\Foundation\Bus\Dispatchable;
1213
use Illuminate\Queue\InteractsWithQueue;
1314
use Illuminate\Queue\SerializesModels;
@@ -31,30 +32,22 @@ class WaitForLinkedProductSku implements ShouldQueue
3132
public $response = [];
3233

3334
/**
34-
* The current number of attempts we have tried to check if the product exists.
35+
* The number of times the job may be attempted.
3536
*
3637
* @var int
3738
*/
38-
public $attempts = 1;
39-
40-
/**
41-
* The maximum number of attempts to stop.
42-
*
43-
* @var int
44-
*/
45-
protected $maxAttempts = 3;
39+
public $tries = 3;
4640

4741
/**
4842
* Create a new job instance.
4943
*
5044
* @param \Grayloon\Magento\Models\MagentoProduct $product
5145
* @return void
5246
*/
53-
public function __construct(MagentoProduct $product, $response, $attempts = 1)
47+
public function __construct(MagentoProduct $product, $response)
5448
{
5549
$this->product = $product;
5650
$this->response = $response;
57-
$this->attempts = $attempts;
5851
}
5952

6053
/**
@@ -67,12 +60,7 @@ public function handle()
6760
$checkSku = MagentoProduct::where('sku', $this->response['linked_product_sku'])->first();
6861

6962
if (! $checkSku) {
70-
if ($this->attempts >= $this->maxAttempts) {
71-
throw new Exception('Failed to find a product id with the Sku '.$this->response['linked_product_sku'].
72-
' to link with product id '.$this->product->id.' after '.$this->attempts.' attempts.');
73-
}
74-
75-
return $this->dispatch($this->product, $this->response, $this->attempts++);
63+
throw new ModelNotFoundException("Failed to find a find product {$this->response['linked_product_sku']} to link with product {$this->product->sku}");
7664
}
7765

7866
(new MagentoProducts())->updateProductLink($this->response, $this->product);

src/Support/HasProductLinks.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ trait HasProductLinks
1717
*/
1818
protected function syncProductLinks($links, $product)
1919
{
20+
MagentoProductLink::where('product_id', $product->id)
21+
->delete();
22+
2023
foreach ($links as $link) {
2124
$this->updateProductLink($link, $product);
2225
}
@@ -39,13 +42,12 @@ public function updateProductLink($link, $product)
3942
return WaitForLinkedProductSku::dispatch($product, $link);
4043
}
4144

42-
MagentoProductLink::updateOrCreate([
45+
MagentoProductLink::create([
4346
'product_id' => $product->id,
4447
'related_product_id' => $productLink->id,
45-
], [
46-
'link_type' => $link['link_type'],
47-
'position' => $link['position'],
48-
'synced_at' => now(),
48+
'link_type' => $link['link_type'],
49+
'position' => $link['position'],
50+
'synced_at' => now(),
4951
]);
5052
}
5153
}

tests/Support/HasProductLinkTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ public function test_can_process_empty_links_response()
9191

9292
$this->assertEquals(0, MagentoProductLink::count());
9393
}
94+
95+
/** @test */
96+
public function it_removes_old_product_links()
97+
{
98+
$product = MagentoProductFactory::new()->create([
99+
'id' => 5,
100+
]);
101+
$link = MagentoProductLinkFactory::new()->create([
102+
'product_id' => $product->id,
103+
]);
104+
105+
(new FakeProductLinksSupportingTest)->exposedSyncProductLinks([], $product);
106+
107+
$this->assertEquals(0, MagentoProductLink::count());
108+
}
94109
}
95110

96111
class FakeProductLinksSupportingTest

0 commit comments

Comments
 (0)