Completed
Push — 6.0 ( 7f5681...093e5d )
by liu
03:59
created

Cookie::save()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 20
rs 10
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
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
46
     * 构造方法
47
     * @access public
48
     */
49 8
    public function __construct(array $config = [])
50
    {
51 8
        $this->config = array_merge($this->config, array_change_key_case($config));
52 8
    }
53
54 8
    public static function __make(Config $config)
2 ignored issues
show
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...
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
55
    {
56 8
        return new static($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

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