user::__set()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
6
 */
7
8
namespace midgard\portable\api;
9
10
use midgard\portable\storage\connection;
11
use midgard\portable\storage\objectmanager;
12
use midgard\portable\api\error\exception;
13
14
/**
15
 * @property integer $id
16
 * @property string $login
17
 * @property string $password
18
 * @property boolean $active
19
 * @property string $authtype
20
 * @property integer $authtypeid
21
 * @property integer $usertype
22
 * @property string $person
23
 * @property string $guid
24
 */
25
class user extends dbobject
26
{
27
    private $person_object;
28
29
    protected $id;
30
31
    protected array $properties = [];
32
33
    protected $person;
34
35
    protected string $guid = '';
36
37
    protected $login = '';
38
39
    protected $password = '';
40
41
    protected $active = false;
42
43
    protected $authtype = '';
44
45
    protected $authtypeid = 0;
46
47
    protected $usertype = 0;
48
49 23
    public function __construct(array $properties = [])
50
    {
51 23
        if (!empty($properties)) {
52 5
            $this->load_by_properties($properties);
53
        }
54
    }
55
56 21
    public function __set($field, $value)
57
    {
58 21
        if ($field == 'guid') {
59 1
            return;
60
        }
61 21
        parent::__set($field, $value);
62
    }
63
64 5
    private function load_by_properties(array $properties)
65
    {
66 5
        if (   !array_key_exists('authtype', $properties)
67 5
            || !array_key_exists('login', $properties)) {
68 1
            throw exception::invalid_property_value();
69
        }
70 4
        $entity = connection::get_em()->getRepository(connection::get_fqcn('midgard_user'))->findOneBy($properties);
71
72 4
        if ($entity === null) {
73 1
            throw exception::not_exists();
74
        }
75 3
        $this->populate_from_entity($entity);
76
    }
77
78 8
    public function login() : bool
79
    {
80 8
        if (empty($this->id)) {
81 1
            return false;
82
        }
83 8
        connection::set_user($this);
84 8
        return true;
85
    }
86
87 1
    public function logout() : bool
88
    {
89 1
        if (empty($this->id)) {
90 1
            return false;
91
        }
92 1
        connection::set_user(null);
93 1
        return true;
94
    }
95
96 1
    public function is_admin() : bool
97
    {
98 1
        return $this->usertype == 2;
99
    }
100
101 10
    public function set_person(person $person)
102
    {
103 10
        $this->person_object = $person;
104 10
        $this->person = $person->guid;
105
    }
106
107 1
    public function &get_person() : person
108
    {
109 1
        if (   $this->person_object === null
110 1
            && $this->person !== null) {
111 1
            $this->person_object = connection::get_em()->getRepository(connection::get_fqcn('midgard_person'))->findOneBy(['guid' => $this->person]);
112
        }
113 1
        return $this->person_object;
114
    }
115
116 20
    public function create() : bool
117
    {
118 20
        if (   empty($this->authtype)
119 20
            || !empty($this->id)) {
120 1
            exception::invalid_property_value();
121 1
            return false;
122
        }
123 20
        if (!$this->is_unique()) {
124 1
            exception::duplicate();
125 1
            return false;
126
        }
127 20
        $this->guid = connection::generate_guid();
128
        try {
129 20
            $om = new objectmanager(connection::get_em());
130 20
            $om->create($this);
131
        } catch (\Exception $e) {
132
            exception::internal($e);
133
            return false;
134
        }
135
136 20
        return !empty($this->id);
137
    }
138
139 1
    public function update() : bool
140
    {
141 1
        if (empty($this->id) || !mgd_is_guid($this->guid)) {
142 1
            exception::invalid_property_value();
143 1
            return false;
144
        }
145 1
        if (!$this->is_unique()) {
146 1
            exception::duplicate();
147 1
            return false;
148
        }
149
        try {
150 1
            $om = new objectmanager(connection::get_em());
151 1
            $om->update($this);
152
        } catch (\Exception $e) {
153
            exception::internal($e);
154
            return false;
155
        }
156 1
        return true;
157
    }
158
159 1
    public function delete() : bool
160
    {
161 1
        if (!mgd_is_guid($this->guid)) {
162 1
            exception::invalid_property_value();
163 1
            return false;
164
        }
165
166
        try {
167 1
            $om = new objectmanager(connection::get_em());
168 1
            $om->purge($this);
169
        } catch (\Exception $e) {
170
            exception::internal($e);
171
            return false;
172
        }
173 1
        $this->guid = '';
174 1
        return true;
175
    }
176
177 20
    protected function is_unique() : bool
178
    {
179 20
        if (   empty($this->login)
180 20
            || empty($this->authtype)) {
181 13
            return true;
182
        }
183
184 9
        $qb = connection::get_em()->createQueryBuilder();
185 9
        $qb->from(get_class($this), 'c');
186 9
        $conditions = $qb->expr()->andX();
187 9
        $parameters = [
188 9
            'login' => $this->login,
189 9
            'authtype' => $this->authtype
190 9
        ];
191
192 9
        if ($this->id) {
193 1
            $parameters['id'] = $this->id;
194 1
            $conditions->add($qb->expr()->neq('c.id', ':id'));
195
        }
196 9
        $conditions->add($qb->expr()->eq('c.login', ':login'));
197 9
        $conditions->add($qb->expr()->eq('c.authtype', ':authtype'));
198
199 9
        $qb->where($conditions)
200 9
            ->setParameters($parameters);
201
202 9
        $qb->select("count(c)");
203 9
        $count = (int) $qb->getQuery()->getSingleScalarResult();
204
205 9
        return $count === 0;
206
    }
207
}
208