Processor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 88
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A processRedirect() 0 13 2
A verifyToken() 0 22 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cakasim\Payone\Sdk\Redirect\Processor;
6
7
use Cakasim\Payone\Sdk\Config\ConfigExceptionInterface;
8
use Cakasim\Payone\Sdk\Config\ConfigInterface;
9
use Cakasim\Payone\Sdk\Redirect\Context\Context;
10
use Cakasim\Payone\Sdk\Redirect\Handler\HandlerManagerInterface;
11
use Cakasim\Payone\Sdk\Redirect\Token\Format\DecoderExceptionInterface;
12
use Cakasim\Payone\Sdk\Redirect\Token\Format\DecoderInterface;
13
use Cakasim\Payone\Sdk\Redirect\Token\TokenInterface;
14
use Psr\Log\LoggerInterface;
15
16
/**
17
 * @author Fabian Böttcher <[email protected]>
18
 * @since 0.1.0
19
 */
20
class Processor implements ProcessorInterface
21
{
22
    /**
23
     * @var LoggerInterface The SDK logger.
24
     */
25
    protected $logger;
26
27
    /**
28
     * @var ConfigInterface The SDK config.
29
     */
30
    protected $config;
31
32
    /**
33
     * @var DecoderInterface The redirect token decoder.
34
     */
35
    protected $decoder;
36
37
    /**
38
     * @var HandlerManagerInterface The redirect handler manager.
39
     */
40
    protected $handlerManager;
41
42
    /**
43
     * Constructs the redirect processor.
44
     *
45
     * @param LoggerInterface $logger The SDK logger.
46
     * @param ConfigInterface $config The SDK config.
47
     * @param DecoderInterface $decoder The redirect token decoder.
48
     * @param HandlerManagerInterface $handlerManager The redirect handler manager.
49
     */
50
    public function __construct(
51
        LoggerInterface $logger,
52
        ConfigInterface $config,
53
        DecoderInterface $decoder,
54
        HandlerManagerInterface $handlerManager
55
    ) {
56
        $this->logger = $logger;
57
        $this->config = $config;
58
        $this->decoder = $decoder;
59
        $this->handlerManager = $handlerManager;
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function processRedirect(string $token): void
66
    {
67
        try {
68
            $this->logger->debug(sprintf("Decode PAYONE redirect token '%s'.", $token));
69
            $token = $this->decoder->decode($token);
70
        } catch (DecoderExceptionInterface $e) {
71
            throw new ProcessorException("Failed redirect processing, could not decode the token.", 0, $e);
72
        }
73
74
        $this->verifyToken($token);
75
76
        $context = new Context($token);
77
        $this->handlerManager->forwardRedirect($context);
78
    }
79
80
    /**
81
     * Verifies the provided token.
82
     *
83
     * @param TokenInterface $token The token to verify.
84
     * @throws ProcessorExceptionInterface If the token verification fails.
85
     */
86
    protected function verifyToken(TokenInterface $token): void
87
    {
88
        /** @var int|null $createdAt */
89
        $createdAt = $token->get('created_at');
90
91
        if (!is_int($createdAt)) {
92
            throw new ProcessorException("Failed redirect processing, the provided token has no 'created_at' payload.");
93
        }
94
95
        try {
96
            /** @var int $lifetime */
97
            $lifetime = $this->config->get('redirect.token_lifetime');
98
        } catch (ConfigExceptionInterface $e) {
99
            throw new ProcessorException("Failed redirect processing, could not get token lifetime from configuration.", 0, $e);
100
        }
101
102
        // Calculate token age.
103
        $age = time() - $createdAt;
104
105
        // Throw exception if token age exceeds configured lifetime.
106
        if ($age > $lifetime) {
107
            throw new ProcessorException("Failed redirect processing, the token is expired.");
108
        }
109
    }
110
}
111