Completed
Push — master ( 99b9b3...0bcb3b )
by Kamil
18:37
created

OrderPaymentProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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\Component\Core\OrderProcessing;
13
14
use Sylius\Component\Core\Model\OrderInterface;
15
use Sylius\Component\Core\Model\PaymentInterface;
16
use Sylius\Component\Core\Payment\Exception\NotProvidedOrderPaymentException;
17
use Sylius\Component\Core\Payment\Provider\OrderPaymentProviderInterface;
18
use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface;
19
use Sylius\Component\Order\Processor\OrderProcessorInterface;
20
use Webmozart\Assert\Assert;
21
22
/**
23
 * @author Paweł Jędrzejewski <[email protected]>
24
 * @author Mateusz Zalewski <[email protected]>
25
 * @author Anna Walasek <[email protected]>
26
 */
27
final class OrderPaymentProcessor implements OrderProcessorInterface
28
{
29
    /**
30
     * @var OrderPaymentProviderInterface
31
     */
32
    private $orderPaymentProvider;
33
34
    /**
35
     * @var string
36
     */
37
    private $targetState;
38
39
    /**
40
     * @param OrderPaymentProviderInterface $orderPaymentProvider
41
     * @param string $targetState
42
     */
43
    public function __construct(
44
        OrderPaymentProviderInterface $orderPaymentProvider,
45
        $targetState = PaymentInterface::STATE_CART
46
    ) {
47
        $this->orderPaymentProvider = $orderPaymentProvider;
48
        $this->targetState = $targetState;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function process(BaseOrderInterface $order)
55
    {
56
        /** @var OrderInterface $order */
57
        Assert::isInstanceOf($order, OrderInterface::class);
58
59
        if (OrderInterface::STATE_CANCELLED === $order->getState()) {
60
            return;
61
        }
62
63
        $lastPayment = $order->getLastPayment($this->targetState);
64
        if (null !== $lastPayment) {
65
            $lastPayment->setCurrencyCode($order->getCurrencyCode());
66
            $lastPayment->setAmount($order->getTotal());
67
68
            return;
69
        }
70
71
        try {
72
            $newPayment = $this->orderPaymentProvider->provideOrderPayment($order, $this->targetState);
73
            $order->addPayment($newPayment);
0 ignored issues
show
Bug introduced by
It seems like $newPayment defined by $this->orderPaymentProvi...er, $this->targetState) on line 72 can be null; however, Sylius\Component\Payment...Interface::addPayment() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
74
        } catch (NotProvidedOrderPaymentException $exception) {
75
            return;
76
        }
77
    }
78
}
79