PaymentMethodOrderVisitor::visitOrder()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 * 
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 * 
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\OrderBundle\Visitor;
14
15
use WellCommerce\Bundle\OrderBundle\Entity\Order;
16
use WellCommerce\Bundle\OrderBundle\Entity\ShippingMethod;
17
18
/**
19
 * Class PaymentMethodOrderVisitor
20
 *
21
 * @author  Adam Piotrowski <[email protected]>
22
 */
23
final class PaymentMethodOrderVisitor implements OrderVisitorInterface
24
{
25
    public function visitOrder(Order $order)
26
    {
27
        if (!$order->getShippingMethod() instanceof ShippingMethod) {
28
            $order->setPaymentMethod(null);
29
            
30
            return;
31
        }
32
        
33
        $shippingMethod = $order->getShippingMethod();
34
        $paymentMethods = $shippingMethod->getPaymentMethods();
35
        
36
        if (null === $order->getPaymentMethod() || false === $paymentMethods->contains($order->getPaymentMethod())) {
37
            $order->setPaymentMethod($paymentMethods->first());
38
        }
39
    }
40
}
41