Completed
Push — master ( 0fb6c2...a0d538 )
by Adam
19:02
created

createOrderStatusHistory()   B

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 29
Code Lines 17

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 2
1
<?php
2
3
namespace WellCommerce\Bundle\ShipmentBundle\Adapter;
4
5
use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractContainerAware;
6
use WellCommerce\Bundle\CoreBundle\Manager\ManagerInterface;
7
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatus;
8
use WellCommerce\Bundle\OrderBundle\Entity\OrderStatusHistory;
9
use WellCommerce\Bundle\ShipmentBundle\Entity\Shipment;
10
use WellCommerce\Bundle\ShipmentBundle\Manager\ShipmentManager;
11
12
/**
13
 * Class AbstractShipmentAdapter
14
 *
15
 * @author  Adam Piotrowski <[email protected]>
16
 */
17
abstract class AbstractShipmentAdapter extends AbstractContainerAware implements ShipmentAdapterInterface
18
{
19
    protected $manager;
20
    
21
    public function __construct(ShipmentManager $manager)
22
    {
23
        $this->manager = $manager;
24
    }
25
    
26
    protected function createOrderStatusHistory(Shipment $shipment, array $formValues): OrderStatusHistory
27
    {
28
        /** @var ManagerInterface $manager */
29
        $manager       = $this->get('order_status_history.manager');
30
        $order         = $shipment->getOrder();
31
        $notify        = (bool)$formValues['required_data']['notify'];
32
        $comment       = $formValues['required_data']['comment'];
33
        $orderStatusId = $formValues['required_data']['orderStatus'];
34
        
35
        /** @var OrderStatus $orderStatus */
36
        $orderStatus = $this->get('order_status.repository')->find($orderStatusId);
37
        
38
        if (!$orderStatus instanceof OrderStatus) {
39
            throw new \Exception('Wrong order status given.');
40
        }
41
        
42
        $order->setCurrentStatus($orderStatus);
43
        
44
        /** @var OrderStatusHistory $orderStatusHistory */
45
        $orderStatusHistory = $this->get('order_status_history.factory')->create();
46
        $orderStatusHistory->setNotify($notify);
47
        $orderStatusHistory->setComment($comment);
48
        $orderStatusHistory->setOrder($order);
49
        $orderStatusHistory->setOrderStatus($order->getCurrentStatus());
50
        
51
        $manager->createResource($orderStatusHistory);
52
        
53
        return $orderStatusHistory;
54
    }
55
}
56