Completed
Push — master ( 95e7f1...06fbc7 )
by Kamil
29:12 queued 07:05
created

UpdatePaymentStateExtension   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 14
c 1
b 1
f 0
lcom 1
cbo 7
dl 0
loc 83
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onPreExecute() 0 3 1
A onExecute() 0 3 1
D onPostExecute() 0 37 9
A updatePaymentState() 0 8 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\Bundle\PayumBundle\Extension;
13
14
use Payum\Core\Extension\Context;
15
use Payum\Core\Extension\ExtensionInterface;
16
use Payum\Core\Request\Generic;
17
use Payum\Core\Request\GetStatusInterface;
18
use Payum\Core\Request\Notify;
19
use SM\Factory\FactoryInterface;
20
use Sylius\Bundle\PayumBundle\Request\GetStatus;
21
use Sylius\Component\Payment\Model\PaymentInterface;
22
use Sylius\Component\Payment\PaymentTransitions;
23
24
final class UpdatePaymentStateExtension implements ExtensionInterface
25
{
26
    /**
27
     * @var FactoryInterface
28
     */
29
    protected $factory;
30
31
    /**
32
     * @param FactoryInterface $factory
33
     */
34
    public function __construct(FactoryInterface $factory)
35
    {
36
        $this->factory = $factory;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function onPreExecute(Context $context)
43
    {
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function onExecute(Context $context)
50
    {
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function onPostExecute(Context $context)
57
    {
58
        $previousStack = $context->getPrevious();
59
        $previousStackSize = count($previousStack);
60
        
61
        if ($previousStackSize > 1) {
62
            return;
63
        } 
64
        
65
        if ($previousStackSize === 1) {
66
            $previousActionClassName = get_class($previousStack[0]->getAction());
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $previousActionClassName exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
67
            if (false === stripos($previousActionClassName, 'NotifyNullAction')) {
68
                return;
69
            }
70
        }
71
72
        /** @var Generic $request */
73
        $request = $context->getRequest();
74
        if (false === $request instanceof Generic) {
75
            return;
76
        }
77
78
        if (false === $request instanceof GetStatusInterface && false === $request instanceof Notify) {
79
            return;
80
        }
81
82
        /** @var PaymentInterface $payment */
83
        $payment = $request->getFirstModel();
84
        if (false === $payment instanceof PaymentInterface) {
85
            return;
86
        }
87
88
        $context->getGateway()->execute($status = new GetStatus($payment));
89
        if ($payment->getState() !== $status->getValue()) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $payment->getState() (string) and $status->getValue() (integer) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
90
            $this->updatePaymentState($payment, $status->getValue());
91
        }
92
    }
93
94
    /**
95
     * @param PaymentInterface $payment
96
     * @param string $nextState
97
     */
98
    protected function updatePaymentState(PaymentInterface $payment, $nextState)
99
    {
100
        $stateMachine = $this->factory->get($payment, PaymentTransitions::GRAPH);
101
102
        if (null !== $transition = $stateMachine->getTransitionToState($nextState)) {
103
            $stateMachine->apply($transition);
104
        }
105
    }
106
}
107