canSubscriptionBeFulfilled()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PH\Component\Core\Resolver;
6
7
use SM\Factory\FactoryInterface;
8
use PH\Component\Core\SubscriptionPaymentStates;
9
use PH\Component\Core\Model\SubscriptionInterface;
10
use PH\Component\Core\SubscriptionTransitions;
11
12
final class SubscriptionStateResolver implements StateResolverInterface
13
{
14
    /**
15
     * @var FactoryInterface
16
     */
17
    private $stateMachineFactory;
18
19
    /**
20
     * @param FactoryInterface $stateMachineFactory
21
     */
22
    public function __construct(FactoryInterface $stateMachineFactory)
23
    {
24
        $this->stateMachineFactory = $stateMachineFactory;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     *
30
     * @throws \SM\SMException
31
     */
32
    public function resolve(SubscriptionInterface $subscription)
33
    {
34
        $stateMachine = $this->stateMachineFactory->get($subscription, SubscriptionTransitions::GRAPH);
35
36
        if ($this->canSubscriptionBeFulfilled($subscription) && $stateMachine->can(SubscriptionTransitions::TRANSITION_FULFILL)) {
37
            $stateMachine->apply(SubscriptionTransitions::TRANSITION_FULFILL);
38
        }
39
40
        if ($this->canSubscriptionBeCancelled($subscription) && $stateMachine->can(SubscriptionTransitions::TRANSITION_CANCEL)) {
41
            $stateMachine->apply(SubscriptionTransitions::TRANSITION_CANCEL);
42
        }
43
    }
44
45
    /**
46
     * @param SubscriptionInterface $subscription
47
     *
48
     * @return bool
49
     */
50
    private function canSubscriptionBeFulfilled(SubscriptionInterface $subscription)
51
    {
52
        return SubscriptionPaymentStates::STATE_PAID === $subscription->getPaymentState();
53
    }
54
55
    /**
56
     * @param SubscriptionInterface $subscription
57
     *
58
     * @return bool
59
     */
60
    private function canSubscriptionBeCancelled(SubscriptionInterface $subscription)
61
    {
62
        return SubscriptionPaymentStates::STATE_CANCELLED === $subscription->getPaymentState();
63
    }
64
}
65