Completed
Push — 6.0 ( 77cbae...03c51c )
by liu
03:09
created

Cookie::set()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 9
nop 3
dl 0
loc 20
ccs 0
cts 10
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think;
14
15
use DateTimeInterface;
16
17
/**
18
 * Cookie管理类
19
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
20
class Cookie
21
{
22
    /**
23
     * 配置参数
24
     * @var array
25
     */
26
    protected $config = [
27
        // cookie 保存时间
28
        'expire'   => 0,
29
        // cookie 保存路径
30
        'path'     => '/',
31
        // cookie 有效域名
32
        'domain'   => '',
33
        //  cookie 启用安全传输
34
        'secure'   => false,
35
        // httponly设置
36
        'httponly' => false,
37
    ];
38
39
    /**
40
     * Cookie写入数据
41
     * @var array
42
     */
43
    protected $cookie = [];
44
45
    /**
46
     * 当前Request对象
47
     * @var Request
48
     */
49
    protected $request;
50
51
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
52
     * 构造方法
53
     * @access public
54
     */
55 8
    public function __construct(Request $request, array $config = [])
56
    {
57 8
        $this->request = $request;
58 8
        $this->config  = array_merge($this->config, array_change_key_case($config));
59 8
    }
60
61 8
    public static function __make(Request $request, Config $config)
2 ignored issues
show
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
Coding Style introduced by
Method name "Cookie::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Public method name "Cookie::__make" must not be prefixed with an underscore
Loading history...
62
    {
63 8
        return new static($request, $config->get('cookie'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('cookie') can also be of type null; however, parameter $config of think\Cookie::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

63
        return new static($request, /** @scrutinizer ignore-type */ $config->get('cookie'));
Loading history...
64
    }
65
66
    /**
67
     * 获取cookie
68
     * @access public
69
     * @param  mixed  $name 数据名称
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
70
     * @param  string $default 默认值
71
     * @return mixed
72
     */
73
    public function get(string $name = '', $default = null)
74
    {
75
        return $this->request->cookie($name, $default);
76
    }
77
78
    /**
79
     * 是否存在Cookie参数
80
     * @access public
81
     * @param  string $name 变量名
82
     * @return bool
83
     */
84
    public function has(string $name): bool
85
    {
86
        return $this->request->has($name, 'cookie');
87
    }
88
89
    /**
90
     * Cookie 设置
91
     *
92
     * @access public
93
     * @param  string $name  cookie名称
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 2 found
Loading history...
94
     * @param  string $value cookie值
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
95
     * @param  mixed  $option 可选参数
96
     * @return void
97
     */
98
    public function set(string $name, string $value, $option = null): void
99
    {
100
        // 参数设置(会覆盖黙认设置)
101
        if (!is_null($option)) {
102
            if (is_numeric($option) || $option instanceof DateTimeInterface) {
103
                $option = ['expire' => $option];
104
            }
105
106
            $config = array_merge($this->config, array_change_key_case($option));
107
        } else {
108
            $config = $this->config;
109
        }
110
111
        if ($config['expire'] instanceof DateTimeInterface) {
112
            $expire = $config['expire']->getTimestamp();
113
        } else {
114
            $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
115
        }
116
117
        $this->setCookie($name, $value, $expire, $config);
118
    }
119
120
    /**
121
     * Cookie 保存
122
     *
123
     * @access public
124
     * @param  string $name  cookie名称
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 2 found
Loading history...
125
     * @param  string $value cookie值
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
126
     * @param  int    $expire 有效期
127
     * @param  array  $option 可选参数
128
     * @return void
129
     */
130
    protected function setCookie(string $name, string $value, int $expire, array $option = []): void
131
    {
132
        $this->cookie[$name] = [$value, $expire, $option];
133
    }
134
135
    /**
136
     * 永久保存Cookie数据
137
     * @access public
138
     * @param  string $name  cookie名称
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 2 found
Loading history...
139
     * @param  string $value cookie值
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
140
     * @param  mixed  $option 可选参数 可能会是 null|integer|string
141
     * @return void
142
     */
143
    public function forever(string $name, string $value = '', $option = null): void
144
    {
145
        if (is_null($option) || is_numeric($option)) {
146
            $option = [];
147
        }
148
149
        $option['expire'] = 315360000;
150
151
        $this->set($name, $value, $option);
152
    }
153
154
    /**
155
     * Cookie删除
156
     * @access public
157
     * @param  string $name cookie名称
158
     * @return void
159
     */
160
    public function delete(string $name): void
161
    {
162
        $this->setCookie($name, '', time() - 3600, $this->config);
163
    }
164
165
    /**
166
     * 获取cookie保存数据
167
     * @access public
168
     * @return array
169
     */
170
    public function getCookie(): array
171
    {
172
        return $this->cookie;
173
    }
174
175
    /**
176
     * 保存Cookie
177
     * @access public
178
     * @return void
179
     */
180
    public function save(): void
181
    {
182
        foreach ($this->cookie as $name => $val) {
183
            list($value, $expire, $option) = $val;
184
185
            $this->saveCookie($name, $value, $expire, $option['path'], $option['domain'], $option['secure'] ? true : false, $option['httponly'] ? true : false);
186
        }
187
    }
188
189
    /**
190
     * 保存Cookie
191
     * @access public
192
     * @param  string $name cookie名称
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
193
     * @param  string $value cookie值
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
194
     * @param  int    $expire cookie过期时间
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
195
     * @param  string $path 有效的服务器路径
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
196
     * @param  string $domain 有效域名/子域名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
197
     * @param  bool   $secure 是否仅仅通过HTTPS
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
198
     * @param  bool   $httponly 仅可通过HTTP访问
199
     * @return void
200
     */
201
    protected function saveCookie(string $name, string $value, int $expire, string $path, string $domain, bool $secure, bool $httponly): void
202
    {
203
        setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
204
    }
205
206
}
207