Completed
Branch feature/phpstanLevel3 (de378e)
by Schlaefer
02:30
created

CurrentUser::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 13
rs 9.8333
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\CurrentUser;
14
15
use Bookmarks\Lib\Bookmarks;
16
use Saito\User\CurrentUser\CurrentUserInterface;
17
use Saito\User\LastRefresh\LastRefreshInterface;
18
use Saito\User\ReadPostings\ReadPostingsInterface;
19
use Saito\User\SaitoUser;
20
21
/**
22
 * Implements the current user visiting the forum
23
 */
24
class CurrentUser extends SaitoUser implements CurrentUserInterface
25
{
26
    /**
27
     * Bookmarks manager
28
     *
29
     * @var Bookmarks
30
     */
31
    private $bookmarks = null;
32
33
    /**
34
     * Manages the last refresh/mark entries as read for the current user
35
     *
36
     * @var LastRefreshInterface
37
     */
38
    private $lastRefresh = null;
39
40
    /**
41
     * @var ReadPostingsInterface
42
     */
43
    private $readPostings;
44
45
    /**
46
     * Stores if a user is logged in. Stored individually for performance.
47
     *
48
     * @var bool
49
     */
50
    protected $isLoggedIn = false;
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function setSettings(array $settings)
56
    {
57
        parent::setSettings($settings);
58
59
        $this->isLoggedIn = !empty($settings['id']);
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function set(string $setting, $value)
66
    {
67
        if ($setting === 'id') {
68
            // @td @sm Class should probably immutable anyway. #Can-O-Worms
69
            $this->isLoggedIn = !empty($value);
70
            throw new \RuntimeException(
71
                'You are not allowed to change the user-ID.',
72
                1563729957
73
            );
74
        }
75
76
        parent::set($setting, $value);
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    public function setLastRefresh(LastRefreshInterface $lastRefresh): CurrentUserInterface
83
    {
84
        $this->lastRefresh = $lastRefresh;
85
86
        return $this;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function getLastRefresh(): LastRefreshInterface
93
    {
94
        if ($this->lastRefresh === null) {
95
            throw new \RuntimeException(
96
                'CurrentUser has no LastRefresh. Set it before you get it.',
97
                1563704131
98
            );
99
        }
100
101
        return $this->lastRefresh;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function setReadPostings(ReadPostingsInterface $readPostings): CurrentUserInterface
108
    {
109
        $this->readPostings = $readPostings;
110
111
        return $this;
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117
    public function getReadPostings(): ReadPostingsInterface
118
    {
119
        if ($this->readPostings === null) {
120
            throw new \RuntimeException(
121
                'CurrentUser has no ReadPostings. Set it before you get it.',
122
                1563704132
123
            );
124
        }
125
126
        return $this->readPostings;
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function hasBookmarked($postingId)
133
    {
134
        if ($this->bookmarks === null) {
135
            $this->bookmarks = new Bookmarks($this);
136
        }
137
138
        return $this->bookmarks->isBookmarked($postingId);
139
    }
140
141
    /**
142
     * {@inheritDoc}
143
     */
144
    public function ignores($userId = null)
145
    {
146
        if (!$this->isLoggedIn()) {
147
            return false;
148
        }
149
150
        return isset($this->_settings['ignores'][$userId]);
151
    }
152
153
    /**
154
     * {@inheritDoc}
155
     *
156
     * Time sensitive, might be called a few 100x on entries/index
157
     */
158
    public function isLoggedIn(): bool
159
    {
160
        return $this->isLoggedIn;
161
    }
162
}
163