Passed
Push — master ( 9329b9...3151bd )
by SignpostMarv
03:38
created

CookieMiddleware::PerhapsReconfigureCookie()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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