Completed
Push — master ( 2960e8...660e14 )
by Kamil
05:23 queued 10s
created

OrderFixture::generateItems()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\CoreBundle\Fixture;
15
16
use Doctrine\Common\Persistence\ObjectManager;
17
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
18
use Sylius\Bundle\CoreBundle\Fixture\Factory\OrderExampleFactory;
19
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
20
use Sylius\Component\Core\Checker\OrderPaymentMethodSelectionRequirementCheckerInterface;
21
use Sylius\Component\Core\Checker\OrderShippingMethodSelectionRequirementCheckerInterface;
22
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
23
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
24
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
25
use Sylius\Component\Resource\Factory\FactoryInterface;
26
use Sylius\Component\Resource\Repository\RepositoryInterface;
27
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
28
29
class OrderFixture extends AbstractFixture
30
{
31
    /** @var OrderExampleFactory */
32
    protected $orderExampleFactory;
33
34
    /** @var ObjectManager  */
35
    protected $orderManager;
36
37
    /** @var \Faker\Generator */
38
    private $faker;
39
40
    public function __construct(
41
        FactoryInterface $orderFactory,
42
        FactoryInterface $orderItemFactory,
43
        OrderItemQuantityModifierInterface $orderItemQuantityModifier,
44
        ObjectManager $orderManager,
45
        RepositoryInterface $channelRepository,
46
        RepositoryInterface $customerRepository,
47
        RepositoryInterface $productRepository,
48
        RepositoryInterface $countryRepository,
49
        PaymentMethodRepositoryInterface $paymentMethodRepository,
50
        ShippingMethodRepositoryInterface $shippingMethodRepository,
51
        FactoryInterface $addressFactory,
52
        StateMachineFactoryInterface $stateMachineFactory,
53
        OrderShippingMethodSelectionRequirementCheckerInterface $orderShippingMethodSelectionRequirementChecker,
54
        OrderPaymentMethodSelectionRequirementCheckerInterface $orderPaymentMethodSelectionRequirementChecker,
55
        OrderExampleFactory $orderExampleFactory = null
56
    ) {
57
        if ($orderExampleFactory === null) {
58
            $orderExampleFactory = new OrderExampleFactory(
59
                $orderFactory,
60
                $orderItemFactory,
61
                $orderItemQuantityModifier,
62
                $orderManager,
63
                $channelRepository,
64
                $customerRepository,
65
                $productRepository,
66
                $paymentMethodRepository,
67
                $countryRepository,
68
                $shippingMethodRepository,
69
                $addressFactory,
70
                $stateMachineFactory,
71
                $orderShippingMethodSelectionRequirementChecker,
72
                $orderPaymentMethodSelectionRequirementChecker
73
            );
74
75
            @trigger_error('Use orderExampleFactory. OrderFixture is deprecated since 1.6 and will be prohibited since 2.0.', \E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
76
        }
77
78
        $this->orderManager = $orderManager;
79
        $this->orderExampleFactory = $orderExampleFactory;
80
81
        $this->faker = \Faker\Factory::create();
82
    }
83
84
    public function load(array $options): void
85
    {
86
        $generateDates = $this->generateDates($options['amount']);
87
88
        for ($i = 0; $i < $options['amount']; ++$i) {
89
            $options = array_merge($options, ['complete_date' => array_shift($generateDates)]);
90
91
            $order = $this->orderExampleFactory->create($options);
92
93
            $this->orderManager->persist($order);
94
95
            if (0 === ($i % 50)) {
96
                $this->orderManager->flush();
97
            }
98
        }
99
100
        $this->orderManager->flush();
101
    }
102
103
    public function getName(): string
104
    {
105
        return 'order';
106
    }
107
108
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
109
    {
110
        $optionsNode
111
            ->children()
112
                ->integerNode('amount')->isRequired()->min(0)->end()
113
                ->scalarNode('channel')->cannotBeEmpty()->end()
114
                ->scalarNode('customer')->cannotBeEmpty()->end()
115
                ->scalarNode('country')->cannotBeEmpty()->end()
116
            ->end()
117
        ;
118
    }
119
120
    private function generateDates(int $amount): array
121
    {
122
        $dates = [];
123
124
        for ($i = 0; $i < $amount; ++$i) {
125
            /** @var \DateTimeInterface|array $dates */
126
            $dates[] = $this->faker->dateTimeBetween('-1 years', 'now');
127
        }
128
129
        sort($dates);
130
131
        return $dates;
132
    }
133
}
134