Cookie   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 38
ccs 0
cts 10
cp 0
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A regenerate() 0 7 1
A get() 0 7 2
A forget() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of Lenius Basket, a PHP package to handle
5
 * your shopping basket.
6
 *
7
 * Copyright (c) 2017 Lenius.
8
 * https://github.com/lenius/basket
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 *
13
 * @author Carsten Jonstrup<[email protected]>
14
 * @copyright 2017 Lenius.
15
 *
16
 * @version production
17
 *
18
 * @see https://github.com/lenius/basket
19
 */
20
21
namespace Lenius\Basket\Identifier;
22
23
use Lenius\Basket\IdentifierInterface;
24
25
/**
26
 * Class Cookie.
27
 */
28
class Cookie implements IdentifierInterface
29
{
30
    /**
31
     * Get the current or new unique identifier.
32
     *
33
     * @return string The identifier
34
     */
35
    public function get(): string
36
    {
37
        if (! empty($_COOKIE['cart_identifier'])) {
38
            return $_COOKIE['cart_identifier'];
39
        }
40
41
        return $this->regenerate();
42
    }
43
44
    /**
45
     * Regenerate the identifier.
46
     *
47
     * @return string The identifier
48
     */
49
    public function regenerate(): string
50
    {
51
        $identifier = md5(uniqid('', true));
52
53
        @setcookie('cart_identifier', $identifier, 0, '/');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for setcookie(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

53
        /** @scrutinizer ignore-unhandled */ @setcookie('cart_identifier', $identifier, 0, '/');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
54
55
        return $identifier;
56
    }
57
58
    /**
59
     * Forget the identifier.
60
     *
61
     * @return void
62
     */
63
    public function forget(): void
64
    {
65
        @setcookie('cart_identifier', '', time() - 3600);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for setcookie(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

65
        /** @scrutinizer ignore-unhandled */ @setcookie('cart_identifier', '', time() - 3600);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
66
    }
67
}
68