Completed
Push — master ( 4f4b30...79d213 )
by Kamil
49:20 queued 33s
created

OrderContext   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
c 2
b 1
f 0
lcom 1
cbo 11
dl 0
loc 136
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A theCustomerPlacedAnOrder() 0 14 1
A theCustomerChoseShippingToWithPayment() 0 21 1
A theCustomerBoughtSingle() 0 16 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
namespace Sylius\Behat\Context\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Doctrine\Common\Persistence\ObjectManager;
16
use Sylius\Bundle\OrderBundle\Modifier\OrderItemQuantityModifierInterface;
17
use Sylius\Component\Core\Model\AddressInterface;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\Component\Core\Model\OrderItemInterface;
20
use Sylius\Component\Core\Model\ProductInterface;
21
use Sylius\Component\Core\Model\ShippingMethodInterface;
22
use Sylius\Component\Core\OrderProcessing\OrderShipmentFactoryInterface;
23
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
24
use Sylius\Component\Core\Test\Services\SharedStorageInterface;
25
use Sylius\Component\Payment\Factory\PaymentFactoryInterface;
26
use Sylius\Component\Payment\Model\PaymentMethodInterface;
27
use Sylius\Component\Resource\Factory\FactoryInterface;
28
use Sylius\Component\User\Model\CustomerInterface;
29
30
/**
31
 * @author Łukasz Chruściel <[email protected]>
32
 */
33
final class OrderContext implements Context
34
{
35
36
    /**
37
     * @var SharedStorageInterface
38
     */
39
    private $sharedStorage;
40
41
    /**
42
     * @var OrderRepositoryInterface
43
     */
44
    private $orderRepository;
45
46
    /**
47
     * @var FactoryInterface
48
     */
49
    private $orderFactory;
50
51
    /**
52
     * @var OrderShipmentFactoryInterface
53
     */
54
    private $orderShipmentFactory;
55
56
    /**
57
     * @var PaymentFactoryInterface
58
     */
59
    private $paymentFactory;
60
61
    /**
62
     * @var FactoryInterface
63
     */
64
    private $orderItemFactory;
65
66
    /**
67
     * @var OrderItemQuantityModifierInterface
68
     */
69
    private $itemQuantityModifier;
70
71
    /**
72
     * @var ObjectManager
73
     */
74
    private $objectManager;
75
76
    /**
77
     * @param OrderRepositoryInterface $orderRepository
78
     * @param FactoryInterface $orderFactory
79
     * @param OrderShipmentFactoryInterface $orderShipmentFactory
80
     * @param PaymentFactoryInterface $paymentFactory
81
     * @param FactoryInterface $orderItemFactory
82
     * @param OrderItemQuantityModifierInterface $itemQuantityModifier
83
     * @param SharedStorageInterface $sharedStorage
84
     * @param ObjectManager $objectManager
85
     */
86
    public function __construct(
87
        SharedStorageInterface $sharedStorage,
88
        OrderRepositoryInterface $orderRepository,
89
        FactoryInterface $orderFactory,
90
        OrderShipmentFactoryInterface $orderShipmentFactory,
91
        PaymentFactoryInterface $paymentFactory,
92
        FactoryInterface $orderItemFactory,
93
        OrderItemQuantityModifierInterface $itemQuantityModifier,
94
        ObjectManager $objectManager
95
    ) {
96
        $this->sharedStorage = $sharedStorage;
97
        $this->orderRepository = $orderRepository;
98
        $this->orderFactory = $orderFactory;
99
        $this->orderShipmentFactory = $orderShipmentFactory;
100
        $this->paymentFactory = $paymentFactory;
101
        $this->orderItemFactory = $orderItemFactory;
102
        $this->itemQuantityModifier = $itemQuantityModifier;
103
        $this->objectManager = $objectManager;
104
    }
105
106
    /**
107
     * @Given the customer :customer placed an order :orderNumber
108
     */
109
    public function theCustomerPlacedAnOrder(CustomerInterface $customer, $orderNumber)
110
    {
111
        /** @var OrderInterface $order */
112
        $order = $this->orderFactory->createNew();
113
114
        $order->setCustomer($customer);
115
        $order->setNumber($orderNumber);
116
        $order->setChannel($this->sharedStorage->get('channel'));
117
        $order->setCurrency($this->sharedStorage->get('currency'));
118
119
        $this->sharedStorage->set('order', $order);
120
121
        $this->orderRepository->add($order);
122
    }
123
124
    /**
125
     * @Given /^the customer chose ("[^"]+" shipping method) (to "[^"]+") with ("[^"]+" payment)$/
126
     */
127
    public function theCustomerChoseShippingToWithPayment(
128
        ShippingMethodInterface $shippingMethod,
129
        AddressInterface $address,
130
        PaymentMethodInterface $paymentMethod
131
    ) {
132
        /** @var OrderInterface $order */
133
        $order = $this->sharedStorage->get('order');
134
135
        $this->orderShipmentFactory->createForOrder($order);
136
        $order->getShipments()->first()->setMethod($shippingMethod);
137
138
        $payment = $this->paymentFactory->createWithAmountAndCurrency($order->getTotal(), $order->getCurrency());
139
        $payment->setMethod($paymentMethod);
140
141
        $order->addPayment($payment);
142
143
        $order->setShippingAddress($address);
144
        $order->setBillingAddress($address);
145
146
        $this->objectManager->flush();
147
    }
148
149
    /**
150
     * @Given the customer bought single :product
151
     */
152
    public function theCustomerBoughtSingle(ProductInterface $product)
153
    {
154
        /** @var OrderInterface $order */
155
        $order = $this->sharedStorage->get('order');
156
157
        /** @var OrderItemInterface $item */
158
        $item = $this->orderItemFactory->createNew();
159
        $item->setVariant($product->getMasterVariant());
160
        $item->setUnitPrice($product->getPrice());
161
162
        $this->itemQuantityModifier->modify($item, 1);
163
164
        $order->addItem($item);
165
166
        $this->objectManager->flush();
167
    }
168
}
169