Completed
Push — develop ( 419626...edd22b )
by Schlaefer
02:31
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\Categories;
17
use Saito\User\CurrentUser\CurrentUserInterface;
18
use Saito\User\LastRefresh\LastRefreshInterface;
19
use Saito\User\Permission;
20
use Saito\User\ReadPostings\ReadPostingsInterface;
21
use Saito\User\SaitoUser;
22
23
/**
24
 * Implements the current user visiting the forum
25
 */
26
class CurrentUser extends SaitoUser implements CurrentUserInterface
27
{
28
    /**
29
     * Bookmarks manager
30
     *
31
     * @var Bookmarks
32
     */
33
    private $bookmarks = null;
34
35
    /**
36
     * Manages the last refresh/mark entries as read for the current user
37
     *
38
     * @var LastRefreshInterface
39
     */
40
    private $lastRefresh = null;
41
42
    /**
43
     * @var ReadPostingsInterface
44
     */
45
    private $readPostings;
46
47
    /**
48
     * Categories
49
     *
50
     * @var Categories
51
     */
52
    private $categories;
53
54
    /**
55
     * Permissions
56
     *
57
     * @var Permission
58
     */
59
    private $permissions;
60
61
    /**
62
     * Stores if a user is logged in. Stored individually for performance.
63
     *
64
     * @var bool
65
     */
66
    protected $isLoggedIn = false;
67
68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function setSettings(array $settings): void
72
    {
73
        parent::setSettings($settings);
74
75
        $this->isLoggedIn = !empty($settings['id']);
76
        $this->permissions = new Permission();
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    public function set(string $setting, $value)
83
    {
84
        if ($setting === 'id') {
85
            // @td @sm Class should probably immutable anyway. #Can-O-Worms
86
            $this->isLoggedIn = !empty($value);
87
            throw new \RuntimeException(
88
                'You are not allowed to change the user-ID.',
89
                1563729957
90
            );
91
        }
92
93
        parent::set($setting, $value);
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    public function setLastRefresh(LastRefreshInterface $lastRefresh): CurrentUserInterface
100
    {
101
        $this->lastRefresh = $lastRefresh;
102
103
        return $this;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109
    public function getLastRefresh(): LastRefreshInterface
110
    {
111
        if ($this->lastRefresh === null) {
112
            throw new \RuntimeException(
113
                'CurrentUser has no LastRefresh. Set it before you get it.',
114
                1563704131
115
            );
116
        }
117
118
        return $this->lastRefresh;
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function setReadPostings(ReadPostingsInterface $readPostings): CurrentUserInterface
125
    {
126
        $this->readPostings = $readPostings;
127
128
        return $this;
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    public function getReadPostings(): ReadPostingsInterface
135
    {
136
        if ($this->readPostings === null) {
137
            throw new \RuntimeException(
138
                'CurrentUser has no ReadPostings. Set it before you get it.',
139
                1563704132
140
            );
141
        }
142
143
        return $this->readPostings;
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149
    public function setCategories(Categories $categories): CurrentUserInterface
150
    {
151
        $this->categories = $categories;
152
153
        return $this;
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     */
159
    public function getCategories(): Categories
160
    {
161
        if ($this->categories === null) {
162
            throw new \RuntimeException(
163
                'CurrentUser has no Categories. Set it before you get it.',
164
                1563704132
165
            );
166
        }
167
168
        return $this->categories;
169
    }
170
171
    /**
172
     * {@inheritDoc}
173
     */
174
    public function hasBookmarked($postingId)
175
    {
176
        if ($this->bookmarks === null) {
177
            $this->bookmarks = new Bookmarks($this);
178
        }
179
180
        return $this->bookmarks->isBookmarked($postingId);
181
    }
182
183
    /**
184
     * {@inheritDoc}
185
     */
186
    public function ignores($userId = null)
187
    {
188
        if (!$this->isLoggedIn()) {
189
            return false;
190
        }
191
192
        return isset($this->_settings['ignores'][$userId]);
193
    }
194
195
    /**
196
     * {@inheritDoc}
197
     *
198
     * Time sensitive, might be called a few 100x on entries/index
199
     */
200
    public function isLoggedIn(): bool
201
    {
202
        return $this->isLoggedIn;
203
    }
204
205
    /**
206
     * {@inheritDoc}
207
     */
208
    public function permission(string $resource): bool
209
    {
210
        return $this->permissions->check($this->getRole(), $resource);
211
    }
212
213
    /**
214
     * {@inheritDoc}
215
     */
216
    public function getPermissions(): Permission
217
    {
218
        return $this->permissions;
219
    }
220
}
221