FigRequestCookies   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 47
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 11 2
A set() 0 7 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\RequestInterface;
9
use function is_callable;
10
11
class FigRequestCookies
12
{
13
    public static function get(RequestInterface $request, string $name, ?string $value = null) : Cookie
14
    {
15
        $cookies = Cookies::fromRequest($request);
16
        $cookie  = $cookies->get($name);
17 1
18
        if ($cookie) {
19 1
            return $cookie;
20 1
        }
21 1
22
        return Cookie::create($name, $value);
23
    }
24
25
    public static function set(RequestInterface $request, Cookie $cookie) : RequestInterface
26
    {
27
        return Cookies::fromRequest($request)
28
            ->with($cookie)
29
            ->renderIntoCookieHeader($request)
30
        ;
31
    }
32
33 1
    public static function modify(RequestInterface $request, string $name, callable $modify) : RequestInterface
34
    {
35 1
        if (! is_callable($modify)) {
36 1
            throw new InvalidArgumentException('$modify must be callable.');
37 1
        }
38 1
39
        $cookies = Cookies::fromRequest($request);
40
        $cookie  = $modify($cookies->has($name)
41
            ? $cookies->get($name)
42
            : Cookie::create($name));
43
44
        return $cookies
45
            ->with($cookie)
46
            ->renderIntoCookieHeader($request)
47
        ;
48 2
    }
49
50 2
    public static function remove(RequestInterface $request, string $name) : RequestInterface
51
    {
52
        return Cookies::fromRequest($request)
53
            ->without($name)
54 2
            ->renderIntoCookieHeader($request)
55 2
        ;
56 2
    }
57
}
58