Passed
Push — master ( bd6808...d74e6b )
by Pol
26:39 queued 23:31
created

Symfony::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @see https://github.com/ecphp
8
 */
9
10
declare(strict_types=1);
11
12
namespace EcPhp\CasBundle\Configuration;
13
14
use EcPhp\CasLib\Configuration\Properties as PsrCasConfiguration;
15
use EcPhp\CasLib\Configuration\PropertiesInterface;
16
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17
use Symfony\Component\Routing\RouterInterface;
18
19
use const FILTER_VALIDATE_URL;
20
21
/**
22
 * Class Symfony.
23
 */
24
final class Symfony implements PropertiesInterface
25
{
26
    /**
27
     * @var \EcPhp\CasLib\Configuration\Properties
28
     */
29
    private $cas;
30
31
    /**
32
     * @var \Symfony\Component\Routing\RouterInterface
33
     */
34
    private $router;
35
36
    /**
37
     * Symfony constructor.
38
     *
39
     * @param array<string, mixed> $properties
40
     */
41 2
    public function __construct(array $properties, RouterInterface $router)
42
    {
43 2
        $this->router = $router;
44 2
        $this->cas = new PsrCasConfiguration($this->routeToUrl($properties));
45
    }
46
47 1
    public function all(): array
48
    {
49 1
        return $this->cas->all();
50
    }
51
52
    /**
53
     * @param mixed $offset
54
     *
55
     * @return bool
56
     */
57
    #[\ReturnTypeWillChange]
58
    public function offsetExists($offset)
59
    {
60
        return $this->cas->offsetExists($offset);
61
    }
62
63
    /**
64
     * @param mixed $offset
65
     *
66
     * @return array<string, mixed>|mixed
67
     */
68
    #[\ReturnTypeWillChange]
69
    public function offsetGet($offset)
70
    {
71
        return $this->cas->offsetGet($offset);
72
    }
73
74
    /**
75
     * @param mixed $offset
76
     * @param mixed $value
77
     */
78
    public function offsetSet($offset, $value): void
79
    {
80
        $this->cas->offsetSet($offset, $value);
81
    }
82
83
    /**
84
     * @param mixed $offset
85
     */
86
    public function offsetUnset($offset): void
87
    {
88
        $this->cas->offsetUnset($offset);
89
    }
90
91
    /**
92
     * Transform Symfony routes into absolute URLs.
93
     *
94
     * @param array<string, mixed> $properties
95
     *   The properties.
96
     *
97
     * @return array<string, mixed>
98
     *   The updated properties.
99
     */
100 2
    private function routeToUrl(array $properties): array
101
    {
102 2
        $properties = $this->updateDefaultParameterRouteToUrl(
103
            $properties,
104
            'pgtUrl'
105
        );
106
107 2
        return $this->updateDefaultParameterRouteToUrl(
108
            $properties,
109
            'service'
110
        );
111
    }
112
113
    /**
114
     * @param array<string, mixed> $properties
115
     *
116
     * @return array<string, mixed>
117
     */
118 2
    private function updateDefaultParameterRouteToUrl(array $properties, string $key): array
119
    {
120 2
        foreach ($properties['protocol'] as $protocolKey => $protocol) {
121 2
            if (false === isset($protocol['default_parameters'][$key])) {
122 2
                continue;
123
            }
124
125 2
            $route = $protocol['default_parameters'][$key];
126
127 2
            if (false === filter_var($route, FILTER_VALIDATE_URL)) {
128
                $route = $this
129 2
                    ->router
130 2
                    ->generate(
131
                        $route,
132 2
                        [],
133
                        UrlGeneratorInterface::ABSOLUTE_URL
134
                    );
135
136 2
                $properties['protocol'][$protocolKey]['default_parameters'][$key] = $route;
137
            }
138
        }
139
140 2
        return $properties;
141
    }
142
}
143