1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Saito\User\LastRefresh; |
14
|
|
|
|
15
|
|
|
use Saito\User\CurrentUser\CurrentUserInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* handles last refresh time for the current user |
19
|
|
|
*/ |
20
|
|
|
abstract class LastRefreshAbstract |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var CurrentUserInterface |
25
|
|
|
*/ |
26
|
|
|
protected $_CurrentUser; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \DateTimeInterface timestamp |
30
|
|
|
*/ |
31
|
|
|
protected $_timestamp = null; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var mixed storage object depending on implementation |
35
|
|
|
*/ |
36
|
|
|
protected $_storage; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* |
41
|
|
|
* @param CurrentUserInterface $CurrentUser current-user |
42
|
|
|
* @param mixed $storage storage a storage handler |
43
|
|
|
*/ |
44
|
|
|
public function __construct(CurrentUserInterface $CurrentUser, $storage = null) |
45
|
|
|
{ |
46
|
|
|
$this->_CurrentUser = $CurrentUser; |
47
|
|
|
$this->_storage = $storage; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Checks if last refresh newer than $timestamp. |
52
|
|
|
* |
53
|
|
|
* Performance sensitive: every posting on /entries/index may be |
54
|
|
|
* tested if new for user. |
55
|
|
|
* |
56
|
|
|
* @param string|\DateTimeInterface $timestamp int unix-timestamp or date as string |
57
|
|
|
* @return mixed bool or null if not determinable |
58
|
|
|
*/ |
59
|
|
|
public function isNewerThan($timestamp) |
60
|
|
|
{ |
61
|
|
|
$lastRefresh = $this->_get(); |
62
|
|
|
// timestamp is not set (or readable): everything is considered new |
63
|
|
|
if ($lastRefresh === false) { |
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $lastRefresh > dateToUnix($timestamp); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* returns last refresh timestamp |
72
|
|
|
* |
73
|
|
|
* @return mixed int if unix timestamp or bool false if uninitialized |
74
|
|
|
*/ |
75
|
|
|
abstract protected function _get(); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Mark last refresh as "now" |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function set(): void |
83
|
|
|
{ |
84
|
|
|
$this->_timestamp = new \DateTimeImmutable(); |
85
|
|
|
$this->_set(); |
86
|
|
|
|
87
|
|
|
// All postings indiviually marked as read are now older than the |
88
|
|
|
// last-refresh timestamp and can be removed. |
89
|
|
|
$this->_CurrentUser->ReadEntries->delete(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set timestamp implementation |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
abstract protected function _set(); |
98
|
|
|
} |
99
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: