Passed
Push — 1.11.x ( 080d0e...e78a37 )
by Angel Fernando Quiroz
10:16
created

Lti13Cookie::getCookie()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 11
rs 10
1
<?php
2
/* For license terms, see /license.txt */
3
4
use Packback\Lti1p3\Interfaces\Cookie as Lti1p3Cookie;
5
6
class Lti13Cookie implements Lti1p3Cookie
7
{
8
    public function getCookie($name)
9
    {
10
        if (isset($_COOKIE[$name])) {
11
            return $_COOKIE[$name];
12
        }
13
        // Look for backup cookie if same site is not supported by the user's browser.
14
        if (isset($_COOKIE["LEGACY_".$name])) {
15
            return $_COOKIE["LEGACY_".$name];
16
        }
17
18
        return false;
19
    }
20
21
    public function setCookie($name, $value, $exp = 3600, $options = []): self
22
    {
23
        $cookieOptions = [
24
            'expires' => time() + $exp,
25
        ];
26
27
        // SameSite none and secure will be required for tools to work inside iframes
28
        $sameSiteOptions = [
29
            'samesite' => 'None',
30
            'secure' => false,
31
            'httponly' => true,
32
        ];
33
34
        setcookie($name, $value, array_merge($cookieOptions, $sameSiteOptions, $options));
35
36
        // Set a second fallback cookie in the event that "SameSite" is not supported
37
        setcookie("LEGACY_".$name, $value, array_merge($cookieOptions, $options));
38
39
        return $this;
40
    }
41
}
42