Completed
Push — master ( b9260b...b1a7da )
by Joachim
06:19
created

StockMovementFixture::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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()
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

85
            ->/** @scrutinizer ignore-call */ end()
Loading history...
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