Completed
Push — 6.0 ( c0e9d9...b80043 )
by liu
02:35
created

Cookie::forever()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 3
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 12
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
class Cookie
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Cookie
Loading history...
16
{
17
    /**
18
     * 配置参数
19
     * @var array
20
     */
21
    protected $config = [
22
        // cookie 名称前缀
23
        'prefix'   => '',
24
        // cookie 保存时间
25
        'expire'   => 0,
26
        // cookie 保存路径
27
        'path'     => '/',
28
        // cookie 有效域名
29
        'domain'   => '',
30
        //  cookie 启用安全传输
31
        'secure'   => false,
32
        // httponly设置
33
        'httponly' => false,
34
    ];
35
36
    /**
37
     * Cookie写入数据
38
     * @var array
39
     */
40
    protected $cookie = [];
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
43
     * 构造方法
44
     * @access public
45
     */
46 8
    public function __construct(array $config = [])
47
    {
48 8
        $this->config = array_merge($this->config, array_change_key_case($config));
49 8
    }
50
51 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...
52
    {
53 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

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