Completed
Push — develop ( 23fb07...aaf888 )
by Fabian
13s queued 11s
created

Service   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 25
c 3
b 0
f 0
dl 0
loc 93
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A processRedirect() 0 3 1
A __construct() 0 10 1
A registerHandler() 0 3 1
A applyRedirectParameters() 0 27 4
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
20
/**
21
 * The redirect service.
22
 *
23
 * @author Fabian Böttcher <[email protected]>
24
 * @since 1.0.0
25
 */
26
class Service extends AbstractService
27
{
28
    /**
29
     * @var UrlGeneratorInterface The redirect URL generator.
30
     */
31
    protected $urlGenerator;
32
33
    /**
34
     * @var HandlerManagerInterface The redirect handler manager.
35
     */
36
    protected $handlerManager;
37
38
    /**
39
     * @var ProcessorInterface The redirect processor.
40
     */
41
    protected $processor;
42
43
    /**
44
     * Constructs the redirect service.
45
     *
46
     * @param UrlGeneratorInterface $urlGenerator The redirect URL generator.
47
     * @param HandlerManagerInterface $handlerManager The redirect handler manager.
48
     * @param ProcessorInterface $processor The redirect processor.
49
     * @inheritDoc
50
     */
51
    public function __construct(
52
        ContainerInterface $container,
53
        UrlGeneratorInterface $urlGenerator,
54
        ProcessorInterface $processor,
55
        HandlerManagerInterface $handlerManager
56
    ) {
57
        parent::__construct($container);
58
        $this->urlGenerator = $urlGenerator;
59
        $this->processor = $processor;
60
        $this->handlerManager = $handlerManager;
61
    }
62
63
    /**
64
     * Applies redirect URL parameters to the provided API request
65
     * if the API request is aware of redirect URL parameters.
66
     *
67
     * @param RequestInterface $request The API request.
68
     * @param array $data The payload data for each of the redirect URLs.
69
     * @throws UrlGeneratorExceptionInterface If redirect URL generation fails.
70
     */
71
    public function applyRedirectParameters(RequestInterface $request, array $data = []): void
72
    {
73
        $parameters = [];
74
        $createdAt = time();
75
76
        if ($request instanceof SuccessUrlAwareInterface) {
77
            $parameters['successurl'] = $this->urlGenerator->generate(array_merge($data, [
78
                'status'     => 'success',
79
                'created_at' => $createdAt,
80
            ]));
81
        }
82
83
        if ($request instanceof ErrorUrlAwareInterface) {
84
            $parameters['errorurl'] = $this->urlGenerator->generate(array_merge($data, [
85
                'status'     => 'error',
86
                'created_at' => $createdAt,
87
            ]));
88
        }
89
90
        if ($request instanceof BackUrlAwareInterface) {
91
            $parameters['backurl'] = $this->urlGenerator->generate(array_merge($data, [
92
                'status'     => 'back',
93
                'created_at' => $createdAt,
94
            ]));
95
        }
96
97
        $request->applyParameters($parameters);
98
    }
99
100
    /**
101
     * Processes an inbound redirect.
102
     *
103
     * @param string $token The encoded redirect token.
104
     * @throws ProcessorExceptionInterface If redirect processing fails.
105
     */
106
    public function processRedirect(string $token): void
107
    {
108
        $this->processor->processRedirect($token);
109
    }
110
111
    /**
112
     * Registers a redirect handler.
113
     *
114
     * @param HandlerInterface $handler The handler to register.
115
     */
116
    public function registerHandler(HandlerInterface $handler): void
117
    {
118
        $this->handlerManager->registerHandler($handler);
119
    }
120
}
121