Completed
Push — master ( 9e334c...959f04 )
by Hong
02:35
created

CookieDriver::syncCookies()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 15
nc 4
nop 0
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Session
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Session\Driver;
16
17
use Phossa2\Shared\Base\ObjectAbstract;
18
use Phossa2\Session\Interfaces\DriverInterface;
19
20
/**
21
 * CookieDriver
22
 *
23
 * Use cookie mechanism to set session id over to the client
24
 *
25
 * @package Phossa2\Session
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     ObjectAbstract
28
 * @see     DriverInterface
29
 * @version 2.1.0
30
 * @since   2.1.0 added
31
 */
32
class CookieDriver extends ObjectAbstract implements DriverInterface
33
{
34
    /**
35
     * @var    array
36
     * @access protected
37
     */
38
    protected $cookies = [];
39
40
    /**
41
     *  cookie settings
42
     *
43
     * @var    string
44
     * @access protected
45
     */
46
    protected $cookie_domain = null;
47
    protected $cookie_path   = '/';
48
    protected $cookie_ttl    = 0;
49
    protected $cookie_secure = false;
50
    protected $cookie_httponly = true;
51
52
    /**
53
     * Constructor
54
     *
55
     * @param  array $settings cookie settings
56
     * @access public
57
     */
58
    public function __construct(array $settings = [])
59
    {
60
        $this->setProperties($settings);
61
    }
62
63
    /**
64
     * Destructor
65
     *
66
     * Set cookies right before script finishes
67
     *
68
     * @access public
69
     */
70
    public function __destruct()
71
    {
72
        $this->syncCookies();
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function get(/*# string */ $sessionName)/*# : string */
79
    {
80
        // get from cookie
81
        if (isset($_COOKIE[$sessionName])) {
82
            return $_COOKIE[$sessionName];
83
        }
84
85
        // returns empty
86
        return '';
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function set(
93
        /*# string */ $sessionName,
94
        /*# string */ $sessionId
95
    )/*# : bool */ {
96
        $this->cookies[$sessionName] = $sessionId;
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    public function del(/*# string */ $sessionName)/*# : bool */
103
    {
104
        if (isset($_COOKIE[$sessionName])) {
105
            unset($_COOKIE[$sessionName]);
106
        }
107
        $this->cookies[$sessionName] = null;
108
    }
109
110
    /**
111
     * Sync session cookies with the client
112
     *
113
     * @access protected
114
     */
115
    protected function syncCookies()
116
    {
117
        if (php_sapi_name() === 'cli') {
118
            return;
119
        }
120
121
        foreach ($this->cookies as $name => $id) {
122
            if (null === $id) {
123
                setcookie($name, '', time() - 3600);
124
            } else {
125
                setcookie(
126
                    $name,
127
                    $id,
128
                    $this->cookie_ttl ? (time() + $this->cookie_ttl) : 0,
129
                    $this->cookie_path,
130
                    $this->cookie_domain,
131
                    $this->cookie_secure,
132
                    $this->cookie_httponly
133
                );
134
            }
135
        }
136
    }
137
}
138