-
Notifications
You must be signed in to change notification settings - Fork 34
/
OrderExampleFactory.php
76 lines (64 loc) · 4.04 KB
/
OrderExampleFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/*
* This file is part of Les-Tilleuls.coop's Click 'N' Collect project.
*
* (c) Les-Tilleuls.coop <contact@les-tilleuls.coop>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace CoopTilleuls\SyliusClickNCollectPlugin\Fixture\Factory;
use CoopTilleuls\SyliusClickNCollectPlugin\CollectionTime\AvailableSlotsComputerInterface;
use CoopTilleuls\SyliusClickNCollectPlugin\Entity\ClickNCollectShipmentInterface;
use Doctrine\ORM\EntityManagerInterface;
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
use Sylius\Bundle\CoreBundle\Fixture\Factory\OrderExampleFactory as BaseOrderExampleFactory;
use Sylius\Component\Core\Checker\OrderPaymentMethodSelectionRequirementCheckerInterface;
use Sylius\Component\Core\Checker\OrderShippingMethodSelectionRequirementCheckerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class OrderExampleFactory extends BaseOrderExampleFactory
{
private AvailableSlotsComputerInterface $availableSlotsComputer;
public function __construct(FactoryInterface $orderFactory, FactoryInterface $orderItemFactory, OrderItemQuantityModifierInterface $orderItemQuantityModifier, EntityManagerInterface $orderManager, RepositoryInterface $channelRepository, RepositoryInterface $customerRepository, ProductRepositoryInterface $productRepository, RepositoryInterface $countryRepository, PaymentMethodRepositoryInterface $paymentMethodRepository, ShippingMethodRepositoryInterface $shippingMethodRepository, FactoryInterface $addressFactory, StateMachineFactoryInterface $stateMachineFactory, OrderShippingMethodSelectionRequirementCheckerInterface $orderShippingMethodSelectionRequirementChecker, OrderPaymentMethodSelectionRequirementCheckerInterface $orderPaymentMethodSelectionRequirementChecker, AvailableSlotsComputerInterface $availableSlotsComputer)
{
parent::__construct($orderFactory, $orderItemFactory, $orderItemQuantityModifier, $orderManager, $channelRepository, $customerRepository, $productRepository, $countryRepository, $paymentMethodRepository, $shippingMethodRepository, $addressFactory, $stateMachineFactory, $orderShippingMethodSelectionRequirementChecker, $orderPaymentMethodSelectionRequirementChecker);
$this->availableSlotsComputer = $availableSlotsComputer;
}
protected function selectShipping(OrderInterface $order, \DateTimeInterface $createdAt): void
{
parent::selectShipping($order, $createdAt);
foreach ($order->getShipments() as $shipment) {
if ((null === $method = $shipment->getMethod()) || 'click_n_collect' !== $method->getCode()) {
continue;
}
$shipment->setLocation($this->faker->randomElement($method->getLocations()));
$this->setCollectionTime($shipment);
}
$this->orderManager->flush();
}
private function setCollectionTime(ClickNCollectShipmentInterface $shipment): void
{
if ($shippedAt = $shipment->getShippedAt()) {
$shippedAtImmutable = \DateTimeImmutable::createFromMutable($shipment);
$startDate = $shippedAtImmutable->sub('-3 days');
$endDate = $shippedAtImmutable->add('+1 days');
} else {
$startDate = null;
$endDate = null;
}
$recurrences = ($this->availableSlotsComputer)($shipment, $shipment->getLocation(), $startDate, $endDate, false, 10);
if (null !== $r = array_pop($recurrences)) {
$shipment->setCollectionTime(\DateTimeImmutable::createFromMutable($r->getStart()));
}
}
}