FigResponseCookies   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 52
ccs 11
cts 15
cp 0.7332
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 11 2
A set() 0 7 1
A expire() 0 4 1
A modify() 0 16 3
A remove() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dflydev\FigCookies;
6
7
use InvalidArgumentException;
8
use Psr\Http\Message\ResponseInterface;
9
use function is_callable;
10
11
class FigResponseCookies
12
{
13
    public static function get(ResponseInterface $response, string $name, ?string $value = null) : SetCookie
14
    {
15
        $setCookies = SetCookies::fromResponse($response);
16
        $cookie     = $setCookies->get($name);
17 1
18
        if ($cookie) {
19 1
            return $cookie;
20 1
        }
21 1
22
        return SetCookie::create($name, $value);
23
    }
24
25
    public static function set(ResponseInterface $response, SetCookie $setCookie) : ResponseInterface
26
    {
27
        return SetCookies::fromResponse($response)
28
            ->with($setCookie)
29
            ->renderIntoSetCookieHeader($response)
30
        ;
31
    }
32
33 1
    public static function expire(ResponseInterface $response, string $cookieName) : ResponseInterface
34
    {
35 1
        return static::set($response, SetCookie::createExpired($cookieName));
36 1
    }
37 1
38 1
    public static function modify(ResponseInterface $response, string $name, callable $modify) : ResponseInterface
39
    {
40
        if (! is_callable($modify)) {
41
            throw new InvalidArgumentException('$modify must be callable.');
42
        }
43
44
        $setCookies = SetCookies::fromResponse($response);
45
        $setCookie  = $modify($setCookies->has($name)
46
            ? $setCookies->get($name)
47
            : SetCookie::create($name));
48
49
        return $setCookies
50
            ->with($setCookie)
51
            ->renderIntoSetCookieHeader($response)
52
        ;
53
    }
54
55
    public static function remove(ResponseInterface $response, string $name) : ResponseInterface
56
    {
57
        return SetCookies::fromResponse($response)
58
            ->without($name)
59 2
            ->renderIntoSetCookieHeader($response)
60
        ;
61 2
    }
62
}
63