Completed
Push — develop ( 419626...edd22b )
by Schlaefer
02:31
created

SaitoUser::permission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
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;
14
15
use Cake\Utility\Hash;
16
use Saito\App\Registry;
17
18
/**
19
 * Represents a registered user with all knowledge stored offline
20
 */
21
class SaitoUser implements ForumsUserInterface
22
{
23
    /**
24
     * User ID
25
     *
26
     * @var int
27
     */
28
    protected $_id = null;
29
30
    /**
31
     * User settings
32
     *
33
     * @var array
34
     */
35
    protected $_settings = null;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param array $settings user-settings
41
     */
42
    public function __construct(?array $settings = null)
43
    {
44
        if ($settings !== null) {
45
            $this->setSettings($settings);
46
        }
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function setSettings(array $settings): void
53
    {
54
        if (!empty($settings['id'])) {
55
            $this->_id = (int)$settings['id'];
56
        }
57
58
        $this->_settings = $settings;
59
60
        /// performance cheat
61
        if (!empty($this->_settings['last_refresh'])) {
62
            $this->_settings['last_refresh_unix'] = dateToUnix($this->_settings['last_refresh']);
63
        }
64
65
        /// performance cheat
66
        // adds a property 'ignores' which in a array holds all users ignored by this users as keys:
67
        // ['<key is user-id of ignored user> => <trueish>, …]
68
        if (!empty($this->_settings['user_ignores'])) {
69
            $this->_settings['ignores'] = array_fill_keys(
70
                Hash::extract(
71
                    $this->_settings,
72
                    'user_ignores.{n}.blocked_user_id'
73
                ),
74
                1
75
            );
76
            unset($this->_settings['user_ignores']);
77
        }
78
    }
79
80
    /**
81
     * Get single user setting.
82
     *
83
     * @param string $setting setting-key
84
     * @return null|mixed null if setting not found
85
     */
86
    public function get($setting)
87
    {
88
        if (!isset($this->_settings[$setting])) {
89
            return null;
90
        }
91
92
        return $this->_settings[$setting];
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function set(string $setting, $value)
99
    {
100
        $this->_settings[$setting] = $value;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106
    public function getSettings(): array
107
    {
108
        return $this->_settings;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function getId(): int
115
    {
116
        return $this->_id;
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122
    public function isUser(ForumsUserInterface $user): bool
123
    {
124
        return $user->getId() === $this->getId();
125
    }
126
127
    /**
128
     * Checks if user is forbidden.
129
     *
130
     * @return bool
131
     */
132
    public function isLocked(): bool
133
    {
134
        return (bool)$this->get('user_lock');
135
    }
136
137
    /**
138
     * Checks if user is forbidden.
139
     *
140
     * @return bool
141
     */
142
    public function isActivated(): bool
143
    {
144
        return !$this->get('activate_code');
145
    }
146
147
    /**
148
     * Get role.
149
     *
150
     * @return string
151
     */
152
    public function getRole(): string
153
    {
154
        return $this->get('user_type');
155
    }
156
}
157