|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Saito\User\ReadPostings; |
|
4
|
|
|
|
|
5
|
|
|
use Saito\User\Cookie\Storage; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Handles read posting by a client side cookie. Used for non logged-in |
|
9
|
|
|
* users. |
|
10
|
|
|
*/ |
|
11
|
|
|
class ReadPostingsCookie extends ReadPostingsAbstract |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Max number of postings in cookie |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $maxPostings = 240; |
|
17
|
|
|
|
|
18
|
|
|
/** @var Storage */ |
|
19
|
|
|
protected $storage; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritDoc} |
|
23
|
|
|
*/ |
|
24
|
|
|
public function set($entries) |
|
25
|
|
|
{ |
|
26
|
|
|
$entries = $this->_prepareForSave($entries); |
|
27
|
|
|
if (empty($entries)) { |
|
28
|
|
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$entries = array_fill_keys($entries, 1); |
|
32
|
|
|
$new = $this->_get() + $entries; |
|
33
|
|
|
if (empty($new)) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
$this->readPostings = $new; |
|
37
|
|
|
|
|
38
|
|
|
$this->_gc(); |
|
39
|
|
|
|
|
40
|
|
|
// make simple string and don't encrypt it to keep cookie small enough |
|
41
|
|
|
// to fit $this->_maxPostings into 4 kB |
|
42
|
|
|
$data = implode('.', array_keys($this->readPostings)); |
|
43
|
|
|
$this->storage->write($data); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritDoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function delete() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->storage->delete(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* limits the number of postings saved in cookie |
|
56
|
|
|
* |
|
57
|
|
|
* cookie size should not exceed 4 kB |
|
58
|
|
|
* |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function _gc() |
|
62
|
|
|
{ |
|
63
|
|
|
$overhead = count($this->readPostings) - $this->maxPostings; |
|
64
|
|
|
if ($overhead < 0) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
ksort($this->readPostings); |
|
68
|
|
|
$this->readPostings = array_slice( |
|
69
|
|
|
$this->readPostings, |
|
70
|
|
|
$overhead, |
|
71
|
|
|
null, |
|
72
|
|
|
true |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritDoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function _get() |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->readPostings !== null) { |
|
82
|
|
|
return $this->readPostings; |
|
83
|
|
|
} |
|
84
|
|
|
$this->readPostings = $this->storage->read(); |
|
|
|
|
|
|
85
|
|
|
if (empty($this->readPostings) |
|
86
|
|
|
|| !preg_match('/^[0-9\.]*$/', $this->readPostings) |
|
87
|
|
|
) { |
|
88
|
|
|
$this->readPostings = []; |
|
89
|
|
|
} else { |
|
90
|
|
|
$this->readPostings = explode('.', $this->readPostings); |
|
91
|
|
|
$this->readPostings = array_fill_keys($this->readPostings, 1); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->readPostings; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..