Completed
Branch feature/phpstanLevel2 (435bc7)
by Schlaefer
02:41
created

LastRefreshAbstract::_parseTimestamp()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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 implements LastRefreshInterface
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
     * {@inheritDoc}
52
     */
53
    public function isNewerThan($timestamp): ?bool
54
    {
55
        $lastRefresh = $this->_get();
56
        // timestamp is not set (or readable): everything is considered new
57
        if ($lastRefresh === false) {
58
            return null;
59
        }
60
61
        return $lastRefresh > dateToUnix($timestamp);
62
    }
63
64
    /**
65
     * returns last refresh timestamp
66
     *
67
     * @return mixed int if unix timestamp or bool false if uninitialized
68
     */
69
    abstract protected function _get();
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function set(): void
75
    {
76
        $this->_timestamp = new \DateTimeImmutable();
77
        $this->_set();
78
79
        // All postings indiviually marked as read are now older than the
80
        // last-refresh timestamp and can be removed.
81
        $this->_CurrentUser->getReadPostings()->delete();
82
    }
83
84
    /**
85
     * Set timestamp implementation
86
     *
87
     * @return void
88
     */
89
    abstract protected function _set();
90
}
91