Completed
Branch feature/phpstanLevel3 (de378e)
by Schlaefer
02:30
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 mixed storage object depending on implementation
30
     */
31
    protected $_storage;
32
33
    /**
34
     * Constructor
35
     *
36
     * @param CurrentUserInterface $CurrentUser current-user
37
     * @param mixed $storage storage a storage handler
38
     */
39
    public function __construct(CurrentUserInterface $CurrentUser, $storage = null)
40
    {
41
        $this->_CurrentUser = $CurrentUser;
42
        $this->_storage = $storage;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function isNewerThan($timestamp): ?bool
49
    {
50
        $lastRefresh = $this->_get();
51
        if ($lastRefresh === null) {
52
            // timestamp is not set (or readable): everything is considered new
53
            return null;
54
        }
55
56
        return $lastRefresh > dateToUnix($timestamp);
57
    }
58
59
    /**
60
     * Returns last refresh timestamp
61
     *
62
     * @return int|null Unix-timestamp or null if last refresh isn't set yet.
63
     */
64
    abstract protected function _get(): ?int;
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function set(): void
70
    {
71
        $timestamp = new \DateTimeImmutable();
72
73
        $this->_set($timestamp);
74
75
        $this->_CurrentUser->set('last_refresh', $timestamp);
76
        // All postings indiviually marked as read are now older than the
77
        // last-refresh timestamp and can be removed.
78
        $this->_CurrentUser->getReadPostings()->delete();
79
    }
80
81
    /**
82
     * Set timestamp implementation
83
     *
84
     * @param \DateTimeImmutable $timestamp timestamp to set
85
     * @return void
86
     */
87
    abstract protected function _set(\DateTimeImmutable $timestamp): void;
88
}
89