SubscriptionPaymentProcessor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 60
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B process() 0 32 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PH\Component\Core\Processor;
6
7
use PH\Component\Core\Model\SubscriptionInterface;
8
use PH\Component\Core\Model\PaymentInterface;
9
use PH\Component\Core\Payment\Provider\SubscriptionPaymentProviderInterface;
10
use PH\Component\Core\SubscriptionPaymentStates;
11
12
final class SubscriptionPaymentProcessor implements SubscriptionProcessorInterface
13
{
14
    /**
15
     * @var SubscriptionPaymentProviderInterface
16
     */
17
    private $subscriptionPaymentProvider;
18
19
    /**
20
     * @var string
21
     */
22
    private $targetState;
23
24
    /**
25
     * @param SubscriptionPaymentProviderInterface $subscriptionPaymentProvider
26
     * @param string                               $targetState
27
     */
28
    public function __construct(
29
        SubscriptionPaymentProviderInterface $subscriptionPaymentProvider,
30
        string $targetState = PaymentInterface::STATE_NEW
31
    ) {
32
        $this->subscriptionPaymentProvider = $subscriptionPaymentProvider;
33
        $this->targetState = $targetState;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function process(SubscriptionInterface $subscription): void
40
    {
41
        if (SubscriptionInterface::STATE_CANCELLED === $subscription->getState()) {
42
            return;
43
        }
44
45
        if (0 === $subscription->getTotal()) {
46
            foreach ($subscription->getPayments(SubscriptionPaymentStates::STATE_NEW) as $payment) {
0 ignored issues
show
Unused Code introduced by
The call to SubscriptionInterface::getPayments() has too many arguments starting with \PH\Component\Core\Subsc...aymentStates::STATE_NEW.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
                $subscription->removePayment($payment);
48
            }
49
50
            return;
51
        }
52
53
        $lastPayment = $subscription->getLastPayment($this->targetState);
54
55
        if (null !== $lastPayment) {
56
            $lastPayment->setCurrencyCode($subscription->getCurrencyCode());
57
            $lastPayment->setAmount($subscription->getTotal());
58
            $lastPayment->setMethod($subscription->getMethod());
59
60
            return;
61
        }
62
63
        try {
64
            $newPayment = $this->subscriptionPaymentProvider
65
                ->provideSubscriptionPayment($subscription, $this->targetState);
66
            $subscription->addPayment($newPayment);
67
        } catch (\InvalidArgumentException $exception) {
68
            return;
69
        }
70
    }
71
}
72