|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Setono\SyliusStockMovementPlugin\Fixture; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
8
|
|
|
use Faker\Factory; |
|
9
|
|
|
use Faker\Generator; |
|
10
|
|
|
use Faker\Provider\Biased; |
|
11
|
|
|
use Money\Currency; |
|
12
|
|
|
use Money\Money; |
|
13
|
|
|
use Setono\SyliusStockMovementPlugin\Factory\StockMovementFactoryInterface; |
|
14
|
|
|
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture; |
|
15
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
|
16
|
|
|
use Sylius\Component\Currency\Model\CurrencyInterface; |
|
17
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
18
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
|
19
|
|
|
|
|
20
|
|
|
final class StockMovementFixture extends AbstractFixture |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var StockMovementFactoryInterface */ |
|
23
|
|
|
private $stockMovementFactory; |
|
24
|
|
|
|
|
25
|
|
|
/** @var ObjectManager */ |
|
26
|
|
|
private $stockMovementManager; |
|
27
|
|
|
|
|
28
|
|
|
/** @var ProductVariantRepositoryInterface */ |
|
29
|
|
|
private $productVariantRepository; |
|
30
|
|
|
|
|
31
|
|
|
/** @var RepositoryInterface */ |
|
32
|
|
|
private $currencyRepository; |
|
33
|
|
|
|
|
34
|
|
|
/** @var Generator */ |
|
35
|
|
|
private $faker; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
StockMovementFactoryInterface $stockMovementFactory, |
|
39
|
|
|
ObjectManager $stockMovementManager, |
|
40
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
|
41
|
|
|
RepositoryInterface $currencyRepository |
|
42
|
|
|
) { |
|
43
|
|
|
$this->stockMovementFactory = $stockMovementFactory; |
|
44
|
|
|
$this->stockMovementManager = $stockMovementManager; |
|
45
|
|
|
$this->productVariantRepository = $productVariantRepository; |
|
46
|
|
|
$this->currencyRepository = $currencyRepository; |
|
47
|
|
|
|
|
48
|
|
|
$this->faker = Factory::create(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getName(): string |
|
52
|
|
|
{ |
|
53
|
|
|
return 'setono_stock_movement'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function load(array $options): void |
|
57
|
|
|
{ |
|
58
|
|
|
$productVariants = $this->productVariantRepository->findAll(); |
|
59
|
|
|
$currencies = $this->currencyRepository->findAll(); |
|
60
|
|
|
|
|
61
|
|
|
for ($i = 0; $i < $options['amount']; ++$i) { |
|
62
|
|
|
$quantity = (int) $this->faker->biasedNumberBetween(0, 10, [Biased::class, 'linearLow']); |
|
63
|
|
|
$productVariant = $this->faker->randomElement($productVariants); |
|
64
|
|
|
$currency = $this->faker->randomElement($currencies); |
|
65
|
|
|
$price = $this->generatePrice($currency); |
|
66
|
|
|
$reference = $this->faker->text(); |
|
67
|
|
|
|
|
68
|
|
|
$stockMovement = $this->stockMovementFactory->createValid($quantity, $productVariant, $price, $reference); |
|
69
|
|
|
|
|
70
|
|
|
$this->stockMovementManager->persist($stockMovement); |
|
71
|
|
|
|
|
72
|
|
|
if (0 === ($i % 50)) { |
|
73
|
|
|
$this->stockMovementManager->flush(); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->stockMovementManager->flush(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function configureOptionsNode(ArrayNodeDefinition $resourceNode): void |
|
81
|
|
|
{ |
|
82
|
|
|
$resourceNode |
|
83
|
|
|
->children() |
|
84
|
|
|
->integerNode('amount')->min(1)->isRequired()->end() |
|
85
|
|
|
->end() |
|
|
|
|
|
|
86
|
|
|
; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function generatePrice(CurrencyInterface $currency): Money |
|
90
|
|
|
{ |
|
91
|
|
|
$amount = $this->faker->numberBetween(100, 10000); |
|
92
|
|
|
|
|
93
|
|
|
return new Money($amount, new Currency($currency->getCode())); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|