Passed
Push — master ( de20ab...dbac49 )
by Ivan
01:27
created

LaravelCookie::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace CodeZero\Cookie\Laravel;
4
5
use CodeZero\Cookie\Cookie;
6
use Illuminate\Cookie\CookieJar;
7
use Illuminate\Http\Request;
8
9
class LaravelCookie implements Cookie
10
{
11
    /**
12
     * Laravel's Request Class
13
     *
14
     * @var \Illuminate\Http\Request
15
     */
16
    protected $request;
17
18
    /**
19
     * Laravel's CookieJar Class
20
     *
21
     * @var \Illuminate\Cookie\CookieJar
22
     */
23
    protected $cookie;
24
25
    /**
26
     * Create a new instance of LaravelCookie
27
     *
28
     * @param \Illuminate\Http\Request $request
29
     * @param \Illuminate\Cookie\CookieJar $cookie
30
     */
31
    public function __construct(Request $request, CookieJar $cookie)
1 ignored issue
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
32
    {
33
        $this->request = $request;
34
        $this->cookie = $cookie;
35
    }
36
37
    /**
38
     * Get the value of a cookie
39
     *
40
     * @param string $cookieName
41
     *
42
     * @return null|string
43
     */
44
    public function get($cookieName)
45
    {
46
        return $this->request->cookie($cookieName);
1 ignored issue
show
Bug Compatibility introduced by
The expression $this->request->cookie($cookieName); of type string|array|null adds the type array to the return on line 46 which is incompatible with the return type declared by the interface CodeZero\Cookie\Cookie::get of type null|string.
Loading history...
47
    }
48
49
    /**
50
     * Store a cookie
51
     *
52
     * @param string $cookieName
53
     * @param string $cookieValue
54
     * @param int $minutes
55
     * @param string $path
56
     * @param string $domain
57
     * @param bool $secure
58
     * @param bool $httpOnly
59
     *
60
     * @return bool
61
     */
62
    public function store($cookieName, $cookieValue, $minutes = 60, $path = "/", $domain = null, $secure = true, $httpOnly = true)
63
    {
64
        $this->cookie->queue($cookieName, $cookieValue, $minutes);
65
66
        return true;
67
    }
68
69
    /**
70
     * Store a cookie for a long, long time
71
     *
72
     * @param string $cookieName
73
     * @param string $cookieValue
74
     * @param string $path
75
     * @param string $domain
76
     * @param bool $secure
77
     * @param bool $httpOnly
78
     *
79
     * @return bool
80
     */
81
    public function forever($cookieName, $cookieValue, $path = '/', $domain = null, $secure = null, $httpOnly = true)
82
    {
83
        $cookie = $this->cookie->forever($cookieName, $cookieValue, 60 * 24 * 365 * 5, $path, $domain, $secure, $httpOnly);
2 ignored issues
show
Bug introduced by
It seems like $domain defined by parameter $domain on line 81 can also be of type string; however, Illuminate\Cookie\CookieJar::forever() does only seem to accept boolean|null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $secure defined by parameter $secure on line 81 can also be of type null; however, Illuminate\Cookie\CookieJar::forever() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
84
        $this->cookie->queue($cookie);
85
86
        return true;
87
    }
88
89
    /**
90
     * Delete a cookie
91
     *
92
     * @param string $cookieName
93
     * @param string $path
94
     * @param string $domain
95
     *
96
     * @return null|bool
97
     */
98
    public function delete($cookieName, $path = '/', $domain = null)
99
    {
100
        $cookie = $this->cookie->forget($cookieName);
101
        $this->cookie->queue($cookie);
102
103
        return true;
104
    }
105
106
    /**
107
     * Check if a cookie exists
108
     *
109
     * @param string $cookieName
110
     *
111
     * @return bool
112
     */
113
    public function exists($cookieName)
114
    {
115
        return $this->get($cookieName) !== null;
116
    }
117
}
118