LaravelCookie   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 58.33%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 42
ccs 7
cts 12
cp 0.5833
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A regenerate() 0 7 1
A forget() 0 3 1
A get() 0 11 4
1
<?php
2
3
namespace Lenius\LaravelEcommerce\Identifier;
4
5
use Illuminate\Support\Facades\Cookie;
6
use Illuminate\Support\Str;
7
use Lenius\Basket\IdentifierInterface;
8
9
/**
10
 * Class LaravelCookie.
11
 */
12
class LaravelCookie implements IdentifierInterface
13
{
14
    /**
15
     * Get the current or new unique identifier.
16
     *
17
     * @return string The identifier
18
     */
19 4
    public function get(): string
20
    {
21 4
        if (request()->hasCookie('cart_identifier')) {
22
            $cookie = request()->cookie('cart_identifier');
23
24
            if (is_string($cookie) && ! empty($cookie)) {
25
                return $cookie;
26
            }
27
        }
28
29 4
        return $this->regenerate();
30
    }
31
32
    /**
33
     * Regenerate the identifier.
34
     *
35
     * @return string The identifier
36
     */
37 4
    public function regenerate(): string
38
    {
39 4
        $identifier = (string) Str::uuid();
40
41 4
        Cookie::queue(cookie('cart_identifier', $identifier, 0, '/'));
42
43 4
        return $identifier;
44
    }
45
46
    /**
47
     * Forget the identifier.
48
     *
49
     * @return void
50
     */
51
    public function forget(): void
52
    {
53
        Cookie::queue(Cookie::forget('cart_identifier'));
54
    }
55
}
56