Passed
Push — master ( 5b49ac...957a79 )
by SignpostMarv
02:45
created

CookieMiddleware   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 81
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
B PerhapsReconfigureCookie() 0 27 4
A PerhapsReconfigureResponseCookies() 0 13 2
B DaftRouterMiddlewareHandler() 0 28 4
A DaftRouterRoutePrefixExceptions() 0 3 1
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftFramework\Http;
8
9
use SignpostMarv\DaftFramework\Framework;
10
use SignpostMarv\DaftRouter\DaftMiddleware;
11
use Symfony\Component\HttpFoundation\Cookie;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class CookieMiddleware implements DaftMiddleware
16
{
17
    public static function DaftRouterMiddlewareHandler(
18
        Request $request,
19
        ? Response $response
20
    ) : ? Response {
21
        if ( ! is_null($response)) {
22
            $framework = Framework::ObtainFrameworkForRequest($request);
23
24
            $config = $framework->ObtainConfig();
25
26
            if ( ! isset($config[self::class])) {
27
                return $response;
28
            }
29
30
            $config = $config[self::class];
31
32
            $configSecure = (bool) ($config['secure'] ?? null);
33
            $configHttpOnly = (bool) ($config['httpOnly'] ?? null);
34
            $configSameSite = is_string($config['sameSite'] ?? null) ? $config['sameSite'] : null;
35
36
            self::PerhapsReconfigureResponseCookies(
37
                $response,
38
                $configSecure,
39
                $configHttpOnly,
40
                $configSameSite
41
            );
42
        }
43
44
        return $response;
45
    }
46
47
    public static function DaftRouterRoutePrefixExceptions() : array
48
    {
49
        return [];
50
    }
51
52
    public static function PerhapsReconfigureResponseCookies(
53
        Response $response,
54
        bool $configSecure,
55
        bool $configHttpOnly,
56
        ? string $configSameSite
57
    ) : void {
58
        foreach ($response->headers->getCookies() as $cookie) {
59
            self::PerhapsReconfigureCookie(
60
                $response,
61
                $cookie,
62
                $configSecure,
63
                $configHttpOnly,
64
                $configSameSite
65
            );
66
        }
67
    }
68
69
    public static function PerhapsReconfigureCookie(
70
        Response $response,
71
        Cookie $cookie,
72
        bool $configSecure,
73
        bool $configHttpOnly,
74
        ? string $configSameSite
75
    ) : void {
76
        $updateSecure = $cookie->isSecure() !== $configSecure;
77
        $updateHttpOnly = $cookie->isHttpOnly() !== $configHttpOnly;
78
        $updateSameSite = $cookie->getSameSite() !== $configSameSite;
79
80
        if ($updateSecure || $updateHttpOnly || $updateSameSite) {
81
            $response->headers->removeCookie(
82
                $cookie->getName(),
83
                $cookie->getPath(),
84
                $cookie->getDomain()
85
            );
86
            $response->headers->setCookie(new Cookie(
87
                $cookie->getName(),
88
                $cookie->getValue(),
89
                $cookie->getExpiresTime(),
90
                $cookie->getPath(),
91
                $cookie->getDomain(),
92
                $configSecure,
93
                $configHttpOnly,
94
                $cookie->isRaw(),
95
                $configSameSite
96
            ));
97
        }
98
    }
99
}
100