SubscriptionStateResolver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 12 5
A canSubscriptionBeFulfilled() 0 4 1
A canSubscriptionBeCancelled() 0 4 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