Passed
Push — master ( f05083...ef147e )
by Gabriel
11:42
created

CookieJar::initDefaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Bytic\Cookie;
4
5
use Nip\Cookie\Cookie;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Bytic\Cookie\Cookie. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
/**
8
 *
9
 */
10
class CookieJar
11
{
12
    /**
13
     * The default path (if specified).
14
     *
15
     * @var string
16
     */
17
    protected $path = '/';
18
19
    /**
20
     * The default domain (if specified).
21
     *
22
     * @var string|null
23
     */
24
    protected $domain;
25
26
    /**
27
     * The default secure setting (defaults to null).
28
     *
29
     * @var bool|null
30
     */
31
    protected $secure;
32
33
    protected $expireTimer = 6 * 60 * 60;
34
35
    /**
36
     * The default SameSite option (defaults to lax).
37
     *
38
     * @var string
39
     */
40
    protected $sameSite = 'lax';
41
42
    public function __construct()
43
    {
44
        $this->initDefaults();
45
    }
46
47
48
    /**
49
     * Create a new cookie instance.
50
     *
51
     * @param string $name
52
     * @param string $value
53
     * @param int $time
54
     * @param string|null $path
55
     * @param string|null $domain
56
     * @param bool|null $secure
57
     * @param bool $httpOnly
58
     * @param bool $raw
59
     * @param string|null $sameSite
60
     * @return \Symfony\Component\HttpFoundation\Cookie
61
     */
62
    public function make(
63
        $name,
64
        $value,
65
        $time = 0,
66
        $path = null,
67
        $domain = null,
68
        $secure = null,
69
        $httpOnly = true,
70
        $raw = false,
71
        $sameSite = null
72
    ) {
73
        $path = $path ?: $this->path;
74
        $domain = $domain ?: $this->domain;
75
        $secure = is_bool($secure) ? $secure : $this->secure;
76
        $sameSite = $sameSite ?: $this->sameSite;
77
        $time = $time ?: time() + $this->expireTimer;
78
        return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
79
    }
80
81
    public function initDefaults()
82
    {
83
        $this->domain = $_SERVER['SERVER_NAME'];
84
    }
85
86
    /**
87
     * @param $defaults
88
     * @return void
89
     */
90
    public function setDefaults($defaults)
91
    {
92
        foreach ($defaults as $name => $value) {
93
            $this->setDefault($name, $value);
94
        }
95
    }
96
97
    /**
98
     * @param $name
99
     * @param $value
100
     * @return void
101
     */
102
    public function setDefault($name, $value = null)
103
    {
104
        if ($value !== null) {
105
            $this->{$name} = $value;
106
        }
107
    }
108
}
109