ResolveNextUrlAction::supports()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PH\Bundle\PayumBundle\Action;
6
7
use Payum\Core\Action\ActionInterface;
8
use PH\Bundle\PayumBundle\Request\ResolveNextUrl;
9
use PH\Component\Core\Model\PaymentInterface;
10
use PH\Component\Core\Model\SubscriptionInterface;
11
12
final class ResolveNextUrlAction implements ActionInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $thankYouUrl;
18
19
    /**
20
     * @var string
21
     */
22
    private $cancelUrl;
23
24
    /**
25
     * ResolveNextUrlAction constructor.
26
     *
27
     * @param string $thankYouUrl
28
     * @param string $cancelUrl
29
     */
30
    public function __construct(string $thankYouUrl, string $cancelUrl)
31
    {
32
        $this->thankYouUrl = $thankYouUrl;
33
        $this->cancelUrl = $cancelUrl;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @param ResolveNextUrl $request
40
     */
41
    public function execute($request): void
42
    {
43
        /** @var PaymentInterface $payment */
44
        $payment = $request->getFirstModel();
45
46
        if (
47
            PaymentInterface::STATE_COMPLETED === $payment->getState() ||
48
            PaymentInterface::STATE_AUTHORIZED === $payment->getState() ||
49
            PaymentInterface::STATE_PROCESSING === $payment->getState()
50
        ) {
51
            $request->setUrl($this->thankYouUrl);
52
53
            /** @var SubscriptionInterface $subscription */
54
            $subscription = $payment->getSubscription();
55
            $params = ['token' => $subscription->getTokenValue()];
56
57
            if (isset($payment->getDetails()['email']) && null !== ($email = $payment->getDetails()['email'])) {
58
                $params['email'] = $email;
59
            }
60
61
            $request->setUrlQueryParams($params);
62
63
            return;
64
        }
65
66
        $request->setUrl($this->cancelUrl);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function supports($request): bool
73
    {
74
        return
75
            $request instanceof ResolveNextUrl &&
76
            $request->getFirstModel() instanceof PaymentInterface
77
        ;
78
    }
79
}
80