Passed
Push — 8.0 ( 3608e0...a90c20 )
by liu
02:14
created

Cookie::saveCookie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 8
dl 0
loc 9
ccs 0
cts 8
cp 0
crap 2
rs 10
c 3
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2023 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
 * @package think
20
 */
21
class Cookie
22
{
23
    /**
24
     * 配置参数
25
     * @var array
26
     */
27
    protected $config = [
28
        // cookie 保存时间
29
        'expire'   => 0,
30
        // cookie 保存路径
31
        'path'     => '/',
32
        // cookie 有效域名
33
        'domain'   => '',
34
        //  cookie 启用安全传输
35
        'secure'   => false,
36
        // httponly设置
37
        'httponly' => false,
38
        // samesite 设置,支持 'strict' 'lax'
39
        'samesite' => '',
40
    ];
41
42
    /**
43
     * Cookie写入数据
44
     * @var array
45
     */
46
    protected $cookie = [];
47
48
    /**
49
     * 构造方法
50
     * @access public
51
     */
52 30
    public function __construct(protected Request $request, array $config = [])
53
    {
54 30
        $this->config = array_merge($this->config, array_change_key_case($config));
55
    }
56
57 30
    public static function __make(Request $request, Config $config)
58
    {
59 30
        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

59
        return new static($request, /** @scrutinizer ignore-type */ $config->get('cookie'));
Loading history...
60
    }
61
62
    /**
63
     * 获取cookie
64
     * @access public
65
     * @param  mixed  $name 数据名称
66
     * @param  string $default 默认值
67
     * @return mixed
68
     */
69
    public function get(string $name = '', $default = null)
70
    {
71
        return $this->request->cookie($name, $default);
72
    }
73
74
    /**
75
     * 是否存在Cookie参数
76
     * @access public
77
     * @param  string $name 变量名
78
     * @return bool
79
     */
80
    public function has(string $name): bool
81
    {
82
        return $this->request->has($name, 'cookie');
83
    }
84
85
    /**
86
     * Cookie 设置
87
     *
88
     * @access public
89
     * @param  string $name  cookie名称
90
     * @param  string $value cookie值
91
     * @param  mixed  $option 可选参数
92
     * @return void
93
     */
94
    public function set(string $name, string $value, $option = null): void
95
    {
96
        // 参数设置(会覆盖黙认设置)
97
        if (!is_null($option)) {
98
            if (is_numeric($option) || $option instanceof DateTimeInterface) {
99
                $option = ['expire' => $option];
100
            }
101
102
            $config = array_merge($this->config, array_change_key_case($option));
103
        } else {
104
            $config = $this->config;
105
        }
106
107
        if ($config['expire'] instanceof DateTimeInterface) {
108
            $expire = $config['expire']->getTimestamp();
109
        } else {
110
            $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
111
        }
112
113
        $this->setCookie($name, $value, $expire, $config);
114
        $this->request->setCookie($name, $value);
115
    }
116
117
    /**
118
     * Cookie 保存
119
     *
120
     * @access public
121
     * @param  string $name  cookie名称
122
     * @param  string $value cookie值
123
     * @param  int    $expire 有效期
124
     * @param  array  $option 可选参数
125
     * @return void
126
     */
127
    protected function setCookie(string $name, string $value, int $expire, array $option = []): void
128
    {
129
        $this->cookie[$name] = [$value, $expire, $option];
130
    }
131
132
    /**
133
     * 永久保存Cookie数据
134
     * @access public
135
     * @param  string $name  cookie名称
136
     * @param  string $value cookie值
137
     * @param  mixed  $option 可选参数 可能会是 null|integer|string
138
     * @return void
139
     */
140
    public function forever(string $name, string $value = '', $option = null): void
141
    {
142
        if (is_null($option) || is_numeric($option)) {
143
            $option = [];
144
        }
145
146
        $option['expire'] = 315360000;
147
148
        $this->set($name, $value, $option);
149
    }
150
151
    /**
152
     * Cookie删除
153
     * @access public
154
     * @param  string $name cookie名称
155
     * @param  array  $options cookie参数
156
     * @return void
157
     */
158
    public function delete(string $name, array $options = []): void
159
    {
160
        $config = array_merge($this->config, array_change_key_case($options));
161
        $this->setCookie($name, '', time() - 3600, $config);
162
        $this->request->setCookie($name, null);
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
            [$value, $expire, $option] = $val;
184
185
            $this->saveCookie(
186
                $name,
187
                $value,
188
                $expire,
189
                $option['path'],
190
                $option['domain'],
191
                $option['secure'] ? true : false,
192
                $option['httponly'] ? true : false,
193
                $option['samesite']
194
            );
195
        }
196
    }
197
198
    /**
199
     * 保存Cookie
200
     * @access public
201
     * @param  string $name cookie名称
202
     * @param  string $value cookie值
203
     * @param  int    $expire cookie过期时间
204
     * @param  string $path 有效的服务器路径
205
     * @param  string $domain 有效域名/子域名
206
     * @param  bool   $secure 是否仅仅通过HTTPS
207
     * @param  bool   $httponly 仅可通过HTTP访问
208
     * @param  string $samesite 防止CSRF攻击和用户追踪
209
     * @return void
210
     */
211
    protected function saveCookie(string $name, string $value, int $expire, string $path, string $domain, bool $secure, bool $httponly, string $samesite): void
212
    {
213
        setcookie($name, $value, [
214
            'expires'  => $expire,
215
            'path'     => $path,
216
            'domain'   => $domain,
217
            'secure'   => $secure,
218
            'httponly' => $httponly,
219
            'samesite' => $samesite,
220
        ]);
221
    }
222
}
223