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

SaitoUser::setSettings()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 10
nop 1
dl 0
loc 38
rs 8.3786
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;
14
15
use App\Model\Entity\User;
16
use Cake\Utility\Hash;
17
use Saito\App\Registry;
18
19
/**
20
 * Represents a registered user with all knowledge stored offline
21
 */
22
class SaitoUser implements ForumsUserInterface
23
{
24
    public $Categories;
25
26
    /**
27
     * User ID
28
     *
29
     * @var int
30
     */
31
    protected $_id = null;
32
33
    /**
34
     * User settings
35
     *
36
     * @var array
37
     */
38
    protected $_settings = null;
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param mixed $settings user-settings
44
     */
45
    public function __construct(array $settings = null)
46
    {
47
        if ($settings !== null) {
48
            $this->setSettings($settings);
49
            $this->Categories = new Categories($this);
50
        }
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function setSettings(array $settings)
57
    {
58
        if (empty($settings)) {
59
            $this->_id = null;
60
            $this->_settings = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $_settings.

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...
61
62
            return;
63
        }
64
65
        if (empty($settings) || !is_array($settings)) {
66
            throw new \RuntimeException("Can't set user.", 1434705388);
67
        }
68
69
        if (!empty($settings['id'])) {
70
            $this->_id = (int)$settings['id'];
71
        }
72
73
        $this->_settings = $settings;
74
75
        // perf-cheat
76
        if (!empty($this->_settings['last_refresh'])) {
77
            $this->_settings['last_refresh_unix'] = dateToUnix($this->_settings['last_refresh']);
78
        }
79
80
        //// performance cheat
81
        // adds a property 'ignores' which in a array holds all users ignored by this users as keys:
82
        // ['<key is user-id of ignored user> => <trueish>, …]
83
        if (!empty($this->_settings['user_ignores'])) {
84
            $this->_settings['ignores'] = array_fill_keys(
85
                Hash::extract(
86
                    $this->_settings,
87
                    'user_ignores.{n}.blocked_user_id'
88
                ),
89
                1
90
            );
91
            unset($this->_settings['user_ignores']);
92
        }
93
    }
94
95
    /**
96
     * Get single user setting.
97
     *
98
     * @param string $setting setting-key
99
     * @return null|mixed null if setting not found
100
     */
101
    public function get($setting)
102
    {
103
        if (!isset($this->_settings[$setting])) {
104
            return null;
105
        }
106
107
        return $this->_settings[$setting];
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113
    public function set(string $setting, $value)
114
    {
115
        $this->_settings[$setting] = $value;
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function getSettings()
122
    {
123
        return $this->_settings;
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129
    public function getId()
130
    {
131
        return $this->_id;
132
    }
133
134
    /**
135
     * {@inheritDoc}
136
     */
137
    public function isUser($user)
138
    {
139
        if (is_numeric($user)) {
140
            $id = (int)$user;
141
        } elseif ($user instanceof ForumsUserInterface || $user instanceof User) {
142
            $id = $user->get('id');
143
        } else {
144
            throw new \InvalidArgumentException("Can't compare users.", 1434704215);
145
        }
146
147
        return $id === $this->getId();
148
    }
149
150
    /**
151
     * Checks if user is forbidden.
152
     *
153
     * @return bool
154
     */
155
    public function isLocked(): bool
156
    {
157
        return (bool)$this->get('user_lock');
158
    }
159
160
    /**
161
     * Checks if user is forbidden.
162
     *
163
     * @return bool
164
     */
165
    public function isActivated() : bool
166
    {
167
        return !$this->get('activate_code');
168
    }
169
170
    /**
171
     * Get role.
172
     *
173
     * @return string
174
     */
175
    public function getRole()
176
    {
177
        if ($this->_id === null) {
178
            return 'anon';
179
        } else {
180
            return $this->get('user_type');
181
        }
182
    }
183
184
    /**
185
     * Check if user has permission to access a resource.
186
     *
187
     * @param string $resource resource
188
     * @return bool
189
     */
190
    public function permission($resource)
191
    {
192
        $permission = Registry::get('Permission');
193
194
        return $permission->check($this->getRole(), $resource);
195
    }
196
}
197