Passed
Push — master ( 622e79...efafc0 )
by Andreas
03:31
created

user::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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