Passed
Push — master ( 4335a1...0d9abf )
by SignpostMarv
02:44
created

CookieMiddleware   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 97.44%

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 38
cts 39
cp 0.9744
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B PerhapsReconfigureCookie() 0 27 4
B DaftRouterMiddlewareHandler() 0 31 5
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 16
    public static function DaftRouterMiddlewareHandler(
18
        Request $request,
19
        ? Response $response
20
    ) : ? Response {
21 16
        if ( ! is_null($response)) {
22 16
            $framework = Framework::ObtainFrameworkForRequest($request);
23
24 16
            $config = $framework->ObtainConfig();
25
26 16
            if ( ! isset($config[self::class])) {
27
                return $response;
28
            }
29
30 16
            $config = $config[self::class];
31
32 16
            $configSecure = (bool) ($config['secure'] ?? null);
33 16
            $configHttpOnly = (bool) ($config['httpOnly'] ?? null);
34 16
            $configSameSite = is_string($config['sameSite'] ?? null) ? $config['sameSite'] : null;
35
36 16
            foreach ($response->headers->getCookies() as $cookie) {
37 16
                self::PerhapsReconfigureCookie(
38 16
                    $response,
39 16
                    $cookie,
40 16
                    $configSecure,
41 16
                    $configHttpOnly,
42 16
                    $configSameSite
0 ignored issues
show
Bug introduced by
It seems like $configSameSite can also be of type null; however, parameter $configSameSite of SignpostMarv\DaftFramewo...hapsReconfigureCookie() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
                    /** @scrutinizer ignore-type */ $configSameSite
Loading history...
43
                );
44
            }
45
        }
46
47 16
        return $response;
48
    }
49
50 2
    public static function DaftRouterRoutePrefixExceptions() : array
51
    {
52 2
        return [];
53
    }
54
55 16
    public static function PerhapsReconfigureCookie(
56
        Response $response,
57
        Cookie $cookie,
58
        bool $configSecure,
59
        bool $configHttpOnly,
60
        string $configSameSite
61
    ) : void {
62 16
        $updateSecure = $cookie->isSecure() !== $configSecure;
63 16
        $updateHttpOnly = $cookie->isHttpOnly() !== $configHttpOnly;
64 16
        $updateSameSite = $cookie->getSameSite() !== $configSameSite;
65
66 16
        if ($updateSecure || $updateHttpOnly || $updateSameSite) {
67 16
            $response->headers->removeCookie(
68 16
                $cookie->getName(),
69 16
                $cookie->getPath(),
70 16
                $cookie->getDomain()
71
            );
72 16
            $response->headers->setCookie(new Cookie(
73 16
                $cookie->getName(),
74 16
                $cookie->getValue(),
75 16
                $cookie->getExpiresTime(),
76 16
                $cookie->getPath(),
77 16
                $cookie->getDomain(),
78 16
                $configSecure,
79 16
                $configHttpOnly,
80 16
                $cookie->isRaw(),
81 16
                $configSameSite
82
            ));
83
        }
84 16
    }
85
}
86