Passed
Push — master ( d042de...4aa687 )
by SignpostMarv
03:15
created

CookieMiddleware::DaftRouterMiddlewareModifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 10
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\DaftRequestInterceptor;
11
use SignpostMarv\DaftRouter\DaftResponseModifier;
12
use Symfony\Component\HttpFoundation\Cookie;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
class CookieMiddleware implements DaftRequestInterceptor, DaftResponseModifier
17
{
18 18
    public static function DaftRouterMiddlewareModifier(
19
        Request $request,
20
        Response $response
21
    ) : Response {
22
        /**
23
        * @var Response $response
24
        */
25 18
        $response = static::OmNomNom($request, $response);
26
27 18
        return $response;
28
    }
29
30 18
    public static function DaftRouterMiddlewareHandler(
31
        Request $request,
32
        ? Response $response
33
    ) : ? Response {
34 18
        return static::OmNomNom($request, $response);
35
    }
36
37 2
    public static function DaftRouterRoutePrefixRequirements() : array
38
    {
39 2
        return [];
40
    }
41
42 2
    public static function DaftRouterRoutePrefixExceptions() : array
43
    {
44 2
        return [];
45
    }
46
47 16
    public static function PerhapsReconfigureResponseCookies(
48
        Response $response,
49
        bool $configSecure,
50
        bool $configHttpOnly,
51
        ? string $configSameSite
52
    ) : void {
53
        /**
54
        * @var Cookie $cookie
55
        */
56 16
        foreach ($response->headers->getCookies() as $cookie) {
57 16
            self::PerhapsReconfigureCookie(
58 16
                $response,
59 16
                $cookie,
60 16
                $configSecure,
61 16
                $configHttpOnly,
62 16
                $configSameSite
63
            );
64
        }
65 16
    }
66
67 16
    public static function PerhapsReconfigureCookie(
68
        Response $response,
69
        Cookie $cookie,
70
        bool $isSecure,
71
        bool $isHttpOnly,
72
        ? string $sameSite
73
    ) : void {
74 16
        $updateSecure = $cookie->isSecure() !== $isSecure;
75 16
        $updateHttpOnly = $cookie->isHttpOnly() !== $isHttpOnly;
76 16
        $updateSameSite = $cookie->getSameSite() !== $sameSite;
77
78 16
        if ($updateSecure || $updateHttpOnly || $updateSameSite) {
79 16
            static::ReconfigureCookie($response, $cookie, $isSecure, $isHttpOnly, $sameSite);
80
        }
81 16
    }
82
83 16
    public static function ReconfigureCookie(
84
        Response $response,
85
        Cookie $cookie,
86
        bool $configSecure,
87
        bool $configHttpOnly,
88
        ? string $configSameSite
89
    ) : void {
90 16
        $cookieName = $cookie->getName();
91 16
        $cookiePath = $cookie->getPath();
92 16
        $cookieDomain = $cookie->getDomain();
93 16
        $response->headers->removeCookie($cookieName, $cookiePath, $cookieDomain);
94 16
        $response->headers->setCookie(new Cookie(
95 16
            $cookieName,
96 16
            $cookie->getValue(),
97 16
            $cookie->getExpiresTime(),
98 16
            $cookiePath,
99 16
            $cookieDomain,
100 16
            $configSecure,
101 16
            $configHttpOnly,
102 16
            $cookie->isRaw(),
103 16
            $configSameSite
104
        ));
105 16
    }
106
107 18
    protected static function OmNomNom(Request $request, ? Response $response) : ? Response
108
    {
109 18
        $config = Framework::ObtainFrameworkForRequest($request)->ObtainConfig();
110 18
        if (isset($response, $config[self::class])) {
111 16
            $config = (array) $config[self::class];
112
113
            /**
114
            * @var string|null $sameSite
115
            */
116 16
            $sameSite = $config['sameSite'] ?? null;
117 16
            $sameSite = is_string($sameSite) ? $sameSite : null;
118 16
            $isSecure = (bool) ($config['secure'] ?? null);
119 16
            $isHttpOnly = (bool) ($config['httpOnly'] ?? null);
120
121 16
            self::PerhapsReconfigureResponseCookies($response, $isSecure, $isHttpOnly, $sameSite);
122
        }
123
124 18
        return $response;
125
    }
126
}
127