Passed
Branch master (b2f909)
by Andreas
04:01
created

user   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Test Coverage

Coverage 90.63%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 101
c 1
b 0
f 0
dl 0
loc 184
ccs 87
cts 96
cp 0.9063
rs 9.68
wmc 34

12 Methods

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