Completed
Branch feature/phpstanLevel2 (bae8ab)
by Schlaefer
02:38
created

LastRefreshDatabase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
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 App\Model\Table\UsersTable;
16
use DateTimeInterface;
17
use Saito\User\CurrentUser\CurrentUserInterface;
18
19
/**
20
 * handles last refresh time for current user via database
21
 *
22
 * used for logged-in users
23
 */
24
class LastRefreshDatabase extends LastRefreshAbstract
25
{
26
    /**
27
     * @var UsersTable
28
     */
29
    protected $storage;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function __construct(CurrentUserInterface $CurrentUser, UsersTable $storage)
35
    {
36
        parent::__construct($CurrentUser, $storage);
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    protected function _get()
43
    {
44
        if ($this->_timestamp === null) {
45
            // can't use ArrayIterator access because array_key_exists doesn't work
46
            // on ArrayIterator … Yeah for PHP!1!!
47
            $settings = $this->_CurrentUser->getSettings();
48
            if (!array_key_exists('last_refresh', $settings)) {
49
                throw new \Exception('last_refresh not set');
50
            } elseif ($settings['last_refresh'] === null) {
51
                // mar is not initialized
52
                $this->_timestamp = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<DateTimeInterface> of property $_timestamp.

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..

Loading history...
53
            } else {
54
                $this->_timestamp = $this->_CurrentUser->get('last_refresh_unix');
55
            }
56
        }
57
58
        return $this->_timestamp;
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    protected function _set()
65
    {
66
        $this->persist($this->_timestamp);
67
        $this->_CurrentUser->set('last_refresh', $this->_timestamp);
68
    }
69
70
    /**
71
     * Set temporary marker
72
     *
73
     * @return void
74
     */
75
    public function setMarker()
76
    {
77
        $this->persist();
78
    }
79
80
    /**
81
     * Persist to strorage
82
     *
83
     * @param DateTimeInterface $timestamp datetime string for last_refresh
84
     * @return void
85
     */
86
    protected function persist(DateTimeInterface $timestamp = null): void
87
    {
88
        $this->_storage->setLastRefresh(
89
            $this->_CurrentUser->getId(),
90
            $timestamp
91
        );
92
    }
93
}
94