Completed
Push — master ( 1af4f5...caaac4 )
by Andreas
03:43
created

user::is_unique()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 30
Code Lines 20

Duplication

Lines 5
Ratio 16.67 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 3
nop 0
dl 5
loc 30
ccs 21
cts 21
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
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 = array();
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
    public static function &get($properties)
0 ignored issues
show
Unused Code introduced by
The parameter $properties is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
    }
42
43
    public static function &query($properties)
0 ignored issues
show
Unused Code introduced by
The parameter $properties is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
    }
46
47 21
    public function __construct(array $properties = array())
48
    {
49 21
        if (!empty($properties)) {
50 5
            $this->load_by_properties($properties);
51 3
        }
52 21
    }
53
54 20
    public function __set($field, $value)
55
    {
56 20
        if ($field == 'guid') {
57 1
            return;
58
        }
59 20
        parent::__set($field, $value);
60 20
    }
61
62 5
    private function load_by_properties(array $properties)
63
    {
64 5
        if (   !array_key_exists('authtype', $properties)
65 5
            || !array_key_exists('login', $properties)) {
66 1
            throw exception::invalid_property_value();
67
        }
68 4
        $entity = connection::get_em()->getRepository('midgard:midgard_user')->findOneBy($properties);
69
70 4
        if ($entity === null) {
71 1
            throw exception::not_exists();
72
        }
73 3
        $this->populate_from_entity($entity);
74 3
    }
75
76 8
    public function login()
77
    {
78 8
        if (empty($this->id)) {
79 1
            return false;
80
        }
81 8
        connection::set_user($this);
82 8
        return true;
83
    }
84
85 1
    public function logout()
86
    {
87 1
        if (empty($this->id)) {
88 1
            return false;
89
        }
90 1
        connection::set_user(null);
91 1
        return true;
92
    }
93
94
    public function is_user()
95
    {
96
        return false;
97
    }
98
99 1
    public function is_admin()
100
    {
101 1
        return ($this->usertype == 2);
102
    }
103
104 9
    public function set_person(person $person)
105
    {
106 9
        $this->person_object = $person;
107 9
        $this->person = $person->guid;
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\person. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108 9
    }
109
110 1
    public function &get_person()
111
    {
112 1
        if (   $this->person_object === null
113 1
            && $this->person !== null) {
114 1
            $this->person_object = connection::get_em()->getRepository('midgard:midgard_person')->findOneBy(array('guid' => $this->person));
115 1
        }
116 1
        return $this->person_object;
117
    }
118
119 19
    public function create()
120
    {
121 19
        if (   empty($this->authtype)
122 19
            || !empty($this->id)) {
123 1
            exception::invalid_property_value();
124 1
            return false;
125
        }
126 19
        if (!$this->is_unique()) {
127 1
            exception::duplicate();
128 1
            return false;
129
        }
130 19
        $this->guid = connection::generate_guid();
131
        try {
132 19
            $om = new objectmanager(connection::get_em());
0 ignored issues
show
Compatibility introduced by
\midgard\portable\storage\connection::get_em() of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
133 19
            $om->create($this);
134 19
        } catch (\Exception $e) {
135
            exception::internal($e);
136
            return false;
137
        }
138
139 19
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
140 19
        return (!empty($this->id));
141
    }
142
143 1 View Code Duplication
    public function update()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145 1
        if (empty($this->id) || !mgd_is_guid($this->guid)) {
146 1
            exception::invalid_property_value();
147 1
            return false;
148
        }
149 1
        if (!$this->is_unique()) {
150 1
            exception::duplicate();
151 1
            return false;
152
        }
153
        try {
154 1
            $om = new objectmanager(connection::get_em());
0 ignored issues
show
Compatibility introduced by
\midgard\portable\storage\connection::get_em() of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
155 1
            $om->update($this);
156 1
        } catch (\Exception $e) {
157
            exception::internal($e);
158
            return false;
159
        }
160 1
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
161 1
        return true;
162
    }
163
164 1
    public function delete()
165
    {
166 1
        if (!mgd_is_guid($this->guid)) {
167 1
            exception::invalid_property_value();
168 1
            return false;
169
        }
170
171
        try {
172 1
            $om = new objectmanager(connection::get_em());
0 ignored issues
show
Compatibility introduced by
\midgard\portable\storage\connection::get_em() of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
173 1
            $om->purge($this);
174 1
        } catch (\Exception $e) {
175
            exception::internal($e);
176
            return false;
177
        }
178 1
        $this->guid = '';
179 1
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
180 1
        return true;
181
    }
182
183 19
    protected function is_unique()
184
    {
185 19
        if (   empty($this->login)
186 19
            || empty($this->authtype)) {
187 13
            return true;
188
        }
189
190 8
        $qb = connection::get_em()->createQueryBuilder();
191 8
        $qb->from(get_class($this), 'c');
192 8
        $conditions = $qb->expr()->andX();
193
        $parameters = array(
194 8
            'login' => $this->login,
195 8
            'authtype' => $this->authtype
196 8
        );
197
198 8 View Code Duplication
        if ($this->id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199 1
            $parameters['id'] = $this->id;
200 1
            $conditions->add($qb->expr()->neq('c.id', ':id'));
201 1
        }
202 8
        $conditions->add($qb->expr()->eq('c.login', ':login'));
203 8
        $conditions->add($qb->expr()->eq('c.authtype', ':authtype'));
204
205 8
        $qb->where($conditions)
206 8
            ->setParameters($parameters);
207
208 8
        $qb->select("count(c)");
209 8
        $count = intval($qb->getQuery()->getSingleScalarResult());
210
211 8
        return ($count === 0);
212
    }
213
}
214