LastRefreshAbstract   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 75
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setMarker() 0 2 1
A __construct() 0 4 1
A isNewerThan() 0 9 2
A set() 0 10 1
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
     * {@inheritDoc}
83
     */
84
    public function setMarker(): void
85
    {
86
    }
87
88
    /**
89
     * Set timestamp implementation
90
     *
91
     * @param \DateTimeImmutable $timestamp timestamp to set
92
     * @return void
93
     */
94
    abstract protected function _set(\DateTimeImmutable $timestamp): void;
95
}
96