LaravelCookie::regenerate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
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