Completed
Push — master ( ead026...e06fe9 )
by Michał
302:23 queued 302:00
created

Sylius/Bundle/CoreBundle/Fixture/OrderFixture.php (8 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
namespace Sylius\Bundle\CoreBundle\Fixture;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
16
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
17
use Sylius\Component\Core\Model\AddressInterface;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\Component\Core\OrderCheckoutTransitions;
20
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Sylius\Component\Resource\Repository\RepositoryInterface;
23
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
24
use Webmozart\Assert\Assert;
25
26
/**
27
 * @author Paweł Jędrzejewski <[email protected]>
28
 */
29
final class OrderFixture extends AbstractFixture
30
{
31
    /**
32
     * @var FactoryInterface
33
     */
34
    private $orderFactory;
35
36
    /**
37
     * @var FactoryInterface
38
     */
39
    private $orderItemFactory;
40
41
    /**
42
     * @var OrderItemQuantityModifierInterface
43
     */
44
    private $orderItemQuantityModifier;
45
46
    /**
47
     * @var ObjectManager
48
     */
49
    private $orderManager;
50
51
    /**
52
     * @var RepositoryInterface
53
     */
54
    private $channelRepository;
55
56
    /**
57
     * @var RepositoryInterface
58
     */
59
    private $customerRepository;
60
61
    /**
62
     * @var RepositoryInterface
63
     */
64
    private $productRepository;
65
66
    /**
67
     * @var RepositoryInterface
68
     */
69
    private $countryRepository;
70
71
    /**
72
     * @var FactoryInterface
73
     */
74
    private $addressFactory;
75
76
    /**
77
     * @var StateMachineFactoryInterface
78
     */
79
    private $stateMachineFactory;
80
81
    /**
82
     * @var \Faker\Generator
83
     */
84
    private $faker;
85
86
    /**
87
     * @param FactoryInterface $orderFactory
88
     * @param FactoryInterface $orderItemFactory
89
     * @param OrderItemQuantityModifierInterface $orderItemQuantityModifier
90
     * @param ObjectManager $orderManager
91
     * @param RepositoryInterface $channelRepository
92
     * @param RepositoryInterface $customerRepository
93
     * @param RepositoryInterface $productRepository
94
     * @param RepositoryInterface $countryRepository
95
     * @param FactoryInterface $addressFactory
96
     * @param StateMachineFactoryInterface $stateMachineFactory
97
     */
98
    public function __construct(
99
        FactoryInterface $orderFactory,
100
        FactoryInterface $orderItemFactory,
101
        OrderItemQuantityModifierInterface $orderItemQuantityModifier,
102
        ObjectManager $orderManager,
103
        RepositoryInterface $channelRepository,
104
        RepositoryInterface $customerRepository,
105
        RepositoryInterface $productRepository,
106
        RepositoryInterface $countryRepository,
107
        FactoryInterface $addressFactory,
108
        StateMachineFactoryInterface $stateMachineFactory
109
    ) {
110
        $this->orderFactory = $orderFactory;
111
        $this->orderItemFactory = $orderItemFactory;
112
        $this->orderItemQuantityModifier = $orderItemQuantityModifier;
113
        $this->orderManager = $orderManager;
114
        $this->channelRepository = $channelRepository;
115
        $this->customerRepository = $customerRepository;
116
        $this->productRepository = $productRepository;
117
        $this->countryRepository = $countryRepository;
118
        $this->addressFactory = $addressFactory;
119
        $this->stateMachineFactory = $stateMachineFactory;
120
121
        $this->faker = \Faker\Factory::create();
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function load(array $options)
128
    {
129
        $channels = $this->channelRepository->findAll();
130
        $customers = $this->customerRepository->findAll();
131
        $countries = $this->countryRepository->findAll();
132
133
        for ($i = 0; $i < $options['amount']; $i++) {
134
            $channel = $this->faker->randomElement($channels);
0 ignored issues
show
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
135
            $customer = $this->faker->randomElement($customers);
0 ignored issues
show
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
136
            $countryCode = $this->faker->randomElement($countries)->getCode();
0 ignored issues
show
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
137
138
            $currencyCode = $this->faker->randomElement($channel->getCurrencies()->toArray())->getCode();
0 ignored issues
show
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
139
140
            $order = $this->orderFactory->createNew();
141
            $order->setChannel($channel);
142
            $order->setCustomer($customer);
143
            $order->setCurrencyCode($currencyCode);
144
145
            $this->generateItems($order);
146
147
            $this->address($order, $countryCode);
148
            $this->selectShipping($order);
149
            $this->selectPayment($order);
150
            $this->completeCheckout($order);
151
152
            $this->orderManager->persist($order);
153
154
            if (0 === ($i % 50)) {
155
                $this->orderManager->flush();
156
            }
157
        }
158
159
        $this->orderManager->flush();
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getName()
166
    {
167
        return 'order';
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
174
    {
175
        $optionsNode
176
            ->children()
177
                ->integerNode('amount')->isRequired()->min(0)->end()
178
        ;
179
    }
180
181
    /**
182
     * @param OrderInterface $order
183
     */
184
    private function generateItems(OrderInterface $order)
185
    {
186
        $numberOfItems = rand(1, 5);
187
        $products = $this->productRepository->findAll();
188
189
        for ($i = 0; $i < $numberOfItems; $i++) {
190
            $item = $this->orderItemFactory->createNew();
191
192
            $product = $this->faker->randomElement($products);
0 ignored issues
show
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
193
            $variant = $this->faker->randomElement($product->getVariants()->toArray());
0 ignored issues
show
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
194
195
            $item->setVariant($variant);
196
            $this->orderItemQuantityModifier->modify($item, rand(1, 5));
197
198
            $order->addItem($item);
199
        }
200
    }
201
202
    /**
203
     * @param OrderInterface $order
204
     * @param string $countryCode
205
     */
206
    private function address(OrderInterface $order, $countryCode)
207
    {
208
        /** @var AddressInterface $address */
209
        $address = $this->addressFactory->createNew();
210
        $address->setFirstname($this->faker->firstName);
211
        $address->setLastname($this->faker->lastName);
212
        $address->setStreet($this->faker->streetName);
213
        $address->setCountryCode($countryCode);
214
        $address->setCity($this->faker->city);
215
        $address->setPostcode($this->faker->postcode);
216
217
        $order->setShippingAddress($address);
218
        $order->setBillingAddress($address);
219
220
        $this->applyCheckoutStateTransition($order, OrderCheckoutTransitions::TRANSITION_ADDRESS);
221
    }
222
223
    /**
224
     * @param OrderInterface $order
225
     */
226
    private function selectShipping(OrderInterface $order)
227
    {
228
        $shippingMethod = $this->faker->randomElement($order->getChannel()->getShippingMethods()->toArray());
0 ignored issues
show
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
229
230
        Assert::notNull($shippingMethod);
231
232
        foreach ($order->getShipments() as $shipment) {
233
            $shipment->setMethod($shippingMethod);
234
        }
235
236
        $this->applyCheckoutStateTransition($order, OrderCheckoutTransitions::TRANSITION_SELECT_SHIPPING);
237
    }
238
239
    /**
240
     * @param OrderInterface $order
241
     */
242
    private function selectPayment(OrderInterface $order)
243
    {
244
        $paymentMethod = $this->faker->randomElement($order->getChannel()->getPaymentMethods()->toArray());
0 ignored issues
show
The call to randomElement() misses some required arguments starting with $'b'.
Loading history...
245
246
        Assert::notNull($paymentMethod);
247
248
        foreach ($order->getPayments() as $payment) {
249
            $payment->setMethod($paymentMethod);
250
        }
251
252
        $this->applyCheckoutStateTransition($order, OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
253
    }
254
255
    /**
256
     * @param OrderInterface $order
257
     */
258
    private function completeCheckout(OrderInterface $order)
259
    {
260
        if ($this->faker->boolean(25)) {
261
            $order->setNotes($this->faker->sentence);
262
        }
263
264
        $this->applyCheckoutStateTransition($order, OrderCheckoutTransitions::TRANSITION_COMPLETE);
265
    }
266
267
    /**
268
     * @param OrderInterface $order
269
     * @param string $transition
270
     */
271
    private function applyCheckoutStateTransition(OrderInterface $order, $transition)
272
    {
273
        $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH)->apply($transition);
274
    }
275
}
276