Completed
Pull Request — develop (#19)
by Fabian
01:28
created

Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
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\UrlGenerator\UrlGeneratorExceptionInterface;
13
use Cakasim\Payone\Sdk\Redirect\UrlGenerator\UrlGeneratorInterface;
14
use Psr\Container\ContainerInterface;
15
16
/**
17
 * The redirect service.
18
 *
19
 * @author Fabian Böttcher <[email protected]>
20
 * @since 1.0.0
21
 */
22
class Service extends AbstractService
23
{
24
    /**
25
     * @var UrlGeneratorInterface The redirect URL generator.
26
     */
27
    protected $urlGenerator;
28
29
    /**
30
     * Constructs the redirect service.
31
     *
32
     * @param UrlGeneratorInterface $urlGenerator The redirect URL generator.
33
     * @inheritDoc
34
     */
35
    public function __construct(
36
        ContainerInterface $container,
37
        UrlGeneratorInterface $urlGenerator
38
    ) {
39
        parent::__construct($container);
40
        $this->urlGenerator = $urlGenerator;
41
    }
42
43
    /**
44
     * Applies redirect URL parameters to the provided API request
45
     * if the API request is aware of redirect URL parameters.
46
     *
47
     * @param RequestInterface $request The API request.
48
     * @param array $data The payload data for each of the redirect URLs.
49
     * @throws UrlGeneratorExceptionInterface If redirect URL generation fails.
50
     */
51
    public function applyRedirectParameters(RequestInterface $request, array $data = []): void
52
    {
53
        $parameters = [];
54
        $createdAt = time();
55
56
        if ($request instanceof SuccessUrlAwareInterface) {
57
            $parameters['successurl'] = $this->urlGenerator->generate(array_merge($data, [
58
                'status'     => 'success',
59
                'created_at' => $createdAt,
60
            ]));
61
        }
62
63
        if ($request instanceof ErrorUrlAwareInterface) {
64
            $parameters['errorurl'] = $this->urlGenerator->generate(array_merge($data, [
65
                'status'     => 'error',
66
                'created_at' => $createdAt,
67
            ]));
68
        }
69
70
        if ($request instanceof BackUrlAwareInterface) {
71
            $parameters['backurl'] = $this->urlGenerator->generate(array_merge($data, [
72
                'status'     => 'back',
73
                'created_at' => $createdAt,
74
            ]));
75
        }
76
77
        $request->applyParameters($parameters);
78
    }
79
}
80