Service::applyRedirectParameters()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 2
b 0
f 0
nc 8
nop 2
dl 0
loc 28
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cakasim\Payone\Sdk\Redirect;
6
7
use Cakasim\Payone\Sdk\AbstractService;
8
use Cakasim\Payone\Sdk\Api\Message\Parameter\BackUrlAwareInterface;
9
use Cakasim\Payone\Sdk\Api\Message\Parameter\ErrorUrlAwareInterface;
10
use Cakasim\Payone\Sdk\Api\Message\Parameter\SuccessUrlAwareInterface;
11
use Cakasim\Payone\Sdk\Api\Message\RequestInterface;
12
use Cakasim\Payone\Sdk\Redirect\Handler\HandlerInterface;
13
use Cakasim\Payone\Sdk\Redirect\Handler\HandlerManagerInterface;
14
use Cakasim\Payone\Sdk\Redirect\Processor\ProcessorExceptionInterface;
15
use Cakasim\Payone\Sdk\Redirect\Processor\ProcessorInterface;
16
use Cakasim\Payone\Sdk\Redirect\UrlGenerator\UrlGeneratorExceptionInterface;
17
use Cakasim\Payone\Sdk\Redirect\UrlGenerator\UrlGeneratorInterface;
18
use Psr\Container\ContainerInterface;
19
use Psr\Log\LoggerInterface;
20
21
/**
22
 * The redirect service.
23
 *
24
 * @author Fabian Böttcher <[email protected]>
25
 * @since 1.0.0
26
 */
27
class Service extends AbstractService
28
{
29
    /**
30
     * @var LoggerInterface The SDK logger.
31
     */
32
    protected $logger;
33
34
    /**
35
     * @var UrlGeneratorInterface The redirect URL generator.
36
     */
37
    protected $urlGenerator;
38
39
    /**
40
     * @var HandlerManagerInterface The redirect handler manager.
41
     */
42
    protected $handlerManager;
43
44
    /**
45
     * @var ProcessorInterface The redirect processor.
46
     */
47
    protected $processor;
48
49
    /**
50
     * Constructs the redirect service.
51
     *
52
     * @param UrlGeneratorInterface $urlGenerator The redirect URL generator.
53
     * @param HandlerManagerInterface $handlerManager The redirect handler manager.
54
     * @param ProcessorInterface $processor The redirect processor.
55
     * @inheritDoc
56
     */
57
    public function __construct(
58
        ContainerInterface $container,
59
        LoggerInterface $logger,
60
        UrlGeneratorInterface $urlGenerator,
61
        ProcessorInterface $processor,
62
        HandlerManagerInterface $handlerManager
63
    ) {
64
        parent::__construct($container);
65
        $this->logger = $logger;
66
        $this->urlGenerator = $urlGenerator;
67
        $this->processor = $processor;
68
        $this->handlerManager = $handlerManager;
69
    }
70
71
    /**
72
     * Applies redirect URL parameters to the provided API request
73
     * if the API request is aware of redirect URL parameters.
74
     *
75
     * @param RequestInterface $request The API request.
76
     * @param array $data The payload data for each of the redirect URLs.
77
     * @throws UrlGeneratorExceptionInterface If redirect URL generation fails.
78
     */
79
    public function applyRedirectParameters(RequestInterface $request, array $data = []): void
80
    {
81
        $parameters = [];
82
        $createdAt = time();
83
84
        if ($request instanceof SuccessUrlAwareInterface) {
85
            $parameters['successurl'] = $this->urlGenerator->generate(array_merge($data, [
86
                'status'     => 'success',
87
                'created_at' => $createdAt,
88
            ]));
89
        }
90
91
        if ($request instanceof ErrorUrlAwareInterface) {
92
            $parameters['errorurl'] = $this->urlGenerator->generate(array_merge($data, [
93
                'status'     => 'error',
94
                'created_at' => $createdAt,
95
            ]));
96
        }
97
98
        if ($request instanceof BackUrlAwareInterface) {
99
            $parameters['backurl'] = $this->urlGenerator->generate(array_merge($data, [
100
                'status'     => 'back',
101
                'created_at' => $createdAt,
102
            ]));
103
        }
104
105
        $this->logger->debug("Apply redirect parameters for PAYONE request.", $parameters);
106
        $request->applyParameters($parameters);
107
    }
108
109
    /**
110
     * Processes an inbound redirect.
111
     *
112
     * @param string $token The encoded redirect token.
113
     * @throws ProcessorExceptionInterface If redirect processing fails.
114
     */
115
    public function processRedirect(string $token): void
116
    {
117
        $this->processor->processRedirect($token);
118
    }
119
120
    /**
121
     * Registers a redirect handler.
122
     *
123
     * @param HandlerInterface $handler The handler to register.
124
     */
125
    public function registerHandler(HandlerInterface $handler): void
126
    {
127
        $this->handlerManager->registerHandler($handler);
128
    }
129
}
130