StockMovementFixture::configureOptionsNode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
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
The method end() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Config...ion\Builder\TreeBuilder. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
            ->/** @scrutinizer ignore-call */ end()
Loading history...
77
        ;
78
    }
79
}
80