SetCookies   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 98
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A has() 0 4 1
A get() 0 8 2
A getAll() 0 4 1
A with() 0 8 1
A without() 0 12 2
A renderIntoSetCookieHeader() 0 9 2
A fromSetCookieStrings() 0 6 1
A fromResponse() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dflydev\FigCookies;
6
7
use Psr\Http\Message\ResponseInterface;
8
use function array_map;
9
use function array_values;
10
11
class SetCookies
12
{
13
    /**
14
     * The name of the Set-Cookie header.
15
     */
16
    public const SET_COOKIE_HEADER = 'Set-Cookie';
17
18
    /** @var SetCookie[] */
19
    private $setCookies = [];
20
21
    /** @param SetCookie[] $setCookies */
22 22
    public function __construct(array $setCookies = [])
23
    {
24 22
        foreach ($setCookies as $setCookie) {
25 19
            $this->setCookies[$setCookie->getName()] = $setCookie;
26 22
        }
27 22
    }
28
29
    public function has(string $name) : bool
30
    {
31
        return isset($this->setCookies[$name]);
32
    }
33 13
34
    public function get(string $name) : ?SetCookie
35 13
    {
36
        if (! $this->has($name)) {
37
            return null;
38
        }
39
40
        return $this->setCookies[$name];
41
    }
42 7
43
    /** @return SetCookie[] */
44 7
    public function getAll() : array
45 1
    {
46
        return array_values($this->setCookies);
47
    }
48 6
49
    public function with(SetCookie $setCookie) : SetCookies
50
    {
51
        $clone = clone($this);
52
53
        $clone->setCookies[$setCookie->getName()] = $setCookie;
54 8
55
        return $clone;
56 8
    }
57
58
    public function without(string $name) : SetCookies
59
    {
60
        $clone = clone($this);
61
62
        if (! $clone->has($name)) {
63 5
            return $clone;
64
        }
65 5
66
        unset($clone->setCookies[$name]);
67 5
68
        return $clone;
69 5
    }
70
71
    /**
72
     * Render SetCookies into a Response.
73
     *
74
     */
75
    public function renderIntoSetCookieHeader(ResponseInterface $response) : ResponseInterface
76 2
    {
77
        $response = $response->withoutHeader(static::SET_COOKIE_HEADER);
78 2
        foreach ($this->setCookies as $setCookie) {
79
            $response = $response->withAddedHeader(static::SET_COOKIE_HEADER, (string) $setCookie);
80 2
        }
81
82
        return $response;
83
    }
84 2
85
    /**
86 2
     * Create SetCookies from a collection of SetCookie header value strings.
87
     *
88
     * @param string[] $setCookieStrings
89
     * @return static
90
     */
91
    public static function fromSetCookieStrings(array $setCookieStrings) : self
92
    {
93
        return new static(array_map(function (string $setCookieString) : SetCookie {
94
            return SetCookie::fromSetCookieString($setCookieString);
95 6
        }, $setCookieStrings));
96
    }
97 6
98 6
    /**
99 6
     * Create SetCookies from a Response.
100 6
     *
101
     */
102 6
    public static function fromResponse(ResponseInterface $response) : SetCookies
103
    {
104
        return new static(array_map(function (string $setCookieString) : SetCookie {
105
            return SetCookie::fromSetCookieString($setCookieString);
106
        }, $response->getHeader(static::SET_COOKIE_HEADER)));
107
    }
108
}
109