LaravelCookie::get()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 5
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 11
ccs 3
cts 6
cp 0.5
crap 6
rs 10
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