Skip to content

Commit 5df85a5

Browse files
authored
Merge pull request #33 from justbetter/feature/defer
Defer retrieve all
2 parents f493145 + 8b80fad commit 5df85a5

File tree

5 files changed

+68
-9
lines changed

5 files changed

+68
-9
lines changed

src/Actions/Retrieval/RetrieveAllStock.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,47 @@
33
namespace JustBetter\MagentoStock\Actions\Retrieval;
44

55
use Illuminate\Support\Carbon;
6+
use Illuminate\Support\Enumerable;
67
use JustBetter\MagentoStock\Contracts\Retrieval\RetrievesAllStock;
78
use JustBetter\MagentoStock\Jobs\Retrieval\RetrieveStockJob;
9+
use JustBetter\MagentoStock\Models\Stock;
810
use JustBetter\MagentoStock\Repositories\BaseRepository;
911

1012
class RetrieveAllStock implements RetrievesAllStock
1113
{
12-
public function retrieve(?Carbon $from): void
14+
public function retrieve(?Carbon $from, bool $defer = true): void
1315
{
1416
$repository = BaseRepository::resolve();
1517

16-
$repository->skus($from)->each(fn (string $sku) => RetrieveStockJob::dispatch($sku));
18+
if (! $defer) {
19+
$repository->skus($from)->each(fn (string $sku) => RetrieveStockJob::dispatch($sku));
20+
21+
return;
22+
}
23+
24+
$date = now();
25+
26+
$repository->skus($from)->chunk(250)->each(function (Enumerable $skus) use ($date): void {
27+
$existing = Stock::query()
28+
->whereIn('sku', $skus)
29+
->pluck('sku');
30+
31+
Stock::query()
32+
->whereIn('sku', $existing)
33+
->update(['retrieve' => true]);
34+
35+
Stock::query()->insert(
36+
$skus
37+
->diff($existing)
38+
->values()
39+
->map(fn (string $sku): array => [
40+
'sku' => $sku,
41+
'retrieve' => true,
42+
'created_at' => $date,
43+
'updated_at' => $date,
44+
])->toArray()
45+
);
46+
});
1747
}
1848

1949
public static function bind(): void

src/Commands/Retrieval/RetrieveAllStockCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class RetrieveAllStockCommand extends Command
1010
{
11-
protected $signature = 'magento-stock:retrieve-all {from?}';
11+
protected $signature = 'magento-stock:retrieve-all {from?} {--queue}';
1212

1313
protected $description = 'Retrieve all (modified) stock';
1414

@@ -17,11 +17,14 @@ public function handle(): int
1717
/** @var ?string $from */
1818
$from = $this->argument('from');
1919

20+
/** @var bool $defer */
21+
$defer = ! $this->option('queue');
22+
2023
$date = $from !== null
2124
? Carbon::parse($from)
2225
: null;
2326

24-
RetrieveAllStockJob::dispatch($date);
27+
RetrieveAllStockJob::dispatch($date, $defer);
2528

2629
return static::SUCCESS;
2730
}

src/Contracts/Retrieval/RetrievesAllStock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface RetrievesAllStock
88
{
9-
public function retrieve(?Carbon $from): void;
9+
public function retrieve(?Carbon $from, bool $defer = true): void;
1010
}

src/Jobs/Retrieval/RetrieveAllStockJob.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class RetrieveAllStockJob implements ShouldBeUnique, ShouldQueue
1616
use InteractsWithQueue;
1717
use Queueable;
1818

19-
public function __construct(public ?Carbon $from = null)
19+
public function __construct(public ?Carbon $from = null, public bool $defer = true)
2020
{
21-
$this->onQueue(config('magento-stock.queue'));
21+
$this->onQueue(config()->string('magento-stock.queue'));
2222
}
2323

2424
public function handle(RetrievesAllStock $stock): void
2525
{
26-
$stock->retrieve($this->from);
26+
$stock->retrieve($this->from, $this->defer);
2727
}
2828
}

tests/Actions/Retrieval/RetrieveAllStockTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\Bus;
66
use JustBetter\MagentoStock\Actions\Retrieval\RetrieveAllStock;
77
use JustBetter\MagentoStock\Jobs\Retrieval\RetrieveStockJob;
8+
use JustBetter\MagentoStock\Models\Stock;
89
use JustBetter\MagentoStock\Tests\Fakes\FakeRepository;
910
use JustBetter\MagentoStock\Tests\TestCase;
1011
use PHPUnit\Framework\Attributes\Test;
@@ -19,8 +20,33 @@ public function it_dispatches_jobs(): void
1920

2021
/** @var RetrieveAllStock $action */
2122
$action = app(RetrieveAllStock::class);
22-
$action->retrieve(null);
23+
$action->retrieve(null, false);
2324

2425
Bus::assertDispatched(RetrieveStockJob::class);
2526
}
27+
28+
#[Test]
29+
public function it_defers_retrievals(): void
30+
{
31+
config()->set('magento-stock.repository', FakeRepository::class);
32+
33+
Bus::fake();
34+
35+
Stock::query()->create(['sku' => 'sku_1', 'retrieve' => false]);
36+
37+
/** @var RetrieveAllStock $action */
38+
$action = app(RetrieveAllStock::class);
39+
$action->retrieve(null, true);
40+
41+
Bus::assertNotDispatched(RetrieveStockJob::class);
42+
43+
$stocks = Stock::query()
44+
->where('retrieve', '=', true)
45+
->pluck('sku');
46+
47+
$this->assertEquals([
48+
'sku_1',
49+
'sku_2',
50+
], $stocks->toArray());
51+
}
2652
}

0 commit comments

Comments
 (0)