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 Setono\SyliusStockMovementPlugin\Factory\StockMovementFactoryInterface; |
||
12 | use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture; |
||
13 | use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
||
14 | use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
||
15 | |||
16 | final class StockMovementFixture extends AbstractFixture |
||
17 | { |
||
18 | /** @var StockMovementFactoryInterface */ |
||
19 | private $stockMovementFactory; |
||
20 | |||
21 | /** @var ObjectManager */ |
||
22 | private $stockMovementManager; |
||
23 | |||
24 | /** @var ProductVariantRepositoryInterface */ |
||
25 | private $productVariantRepository; |
||
26 | |||
27 | /** @var Generator */ |
||
28 | private $faker; |
||
29 | |||
30 | public function __construct( |
||
31 | StockMovementFactoryInterface $stockMovementFactory, |
||
32 | ObjectManager $stockMovementManager, |
||
33 | ProductVariantRepositoryInterface $productVariantRepository |
||
34 | ) { |
||
35 | $this->stockMovementFactory = $stockMovementFactory; |
||
36 | $this->stockMovementManager = $stockMovementManager; |
||
37 | $this->productVariantRepository = $productVariantRepository; |
||
38 | |||
39 | $this->faker = Factory::create(); |
||
40 | } |
||
41 | |||
42 | public function getName(): string |
||
43 | { |
||
44 | return 'setono_stock_movement'; |
||
45 | } |
||
46 | |||
47 | public function load(array $options): void |
||
48 | { |
||
49 | $productVariants = $this->productVariantRepository->findAll(); |
||
50 | |||
51 | for ($i = 0; $i < $options['amount']; ++$i) { |
||
52 | do { |
||
53 | $quantity = (int) $this->faker->biasedNumberBetween(-10, 10, [Biased::class, 'linearLow']); |
||
54 | } while (0 === $quantity); |
||
55 | |||
56 | $productVariant = $this->faker->randomElement($productVariants); |
||
57 | $reference = $this->faker->text(40); |
||
58 | |||
59 | $stockMovement = $this->stockMovementFactory->createValid($quantity, $productVariant, $reference); |
||
60 | |||
61 | $this->stockMovementManager->persist($stockMovement); |
||
62 | |||
63 | if (0 === ($i % 50)) { |
||
64 | $this->stockMovementManager->flush(); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | $this->stockMovementManager->flush(); |
||
69 | } |
||
70 | |||
71 | protected function configureOptionsNode(ArrayNodeDefinition $resourceNode): void |
||
72 | { |
||
73 | $resourceNode |
||
74 | ->children() |
||
75 | ->integerNode('amount')->min(1)->isRequired()->end() |
||
76 | ->end() |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
77 | ; |
||
78 | } |
||
79 | } |
||
80 |