UserActiveRecordModel   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 125
ccs 34
cts 34
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createUser() 0 5 1
A updateUser() 0 6 1
A deleteUser() 0 8 1
A setUserData() 0 10 3
A getUserByField() 0 4 1
A findAllUsers() 0 10 1
1
<?php
2
3
namespace Peto16\User;
4
5
use \Anax\Database\ActiveRecordModel;
6
use \Peto16\User\UserStorageInterface;
7
8
/**
9
 * Class to handle a user.
10
 */
11
class UserActiveRecordModel extends ActiveRecordModel implements UserStorageInterface
12
{
13
14
    /**
15
    * @var string $tableName name of the database table.
16
    */
17
    protected $tableName = "ramverk1_User";
18
19
    /**
20
     * Columns in the table.
21
     *
22
     * @var integer $id primary key auto incremented.
23
     */
24
25
    public $id;
26
    public $username;
27
    public $password;
28
    public $email;
29
    public $firstname;
30
    public $lastname;
31
    public $administrator;
32
    public $enabled;
33
    public $deleted;
34
35
36
37
    /**
38
     * Create user.
39
     *
40
     * @param  User        $user a user object.
41
     *
42
     * @return void
43
     */
44 1
    public function createUser(User $user)
45
    {
46 1
        $this->setUserData($user);
47 1
        $this->save();
48 1
    }
49
50
51
52
    /**
53
     * Update user.
54
     *
55
     * @param  User        $user User object.
56
     *
57
     * @return void
58
     */
59 2
    public function updateUser(User $user)
60
    {
61 2
        $this->find("id", $user->id);
62 2
        $this->setUserData($user);
63 2
        $this->save();
64 2
    }
65
66
67
68
    /**
69
     * Delete user.
70
     *
71
     * @param  integer        $id integer for userid.
72
     *
73
     * @return void
74
     */
75 2
    public function deleteUser($id)
76
    {
77 2
        $user = $this->find("id", $id);
78 2
        $this->id = $user->id;
79 2
        date_default_timezone_set("Europe/Stockholm");
80 2
        $this->deleted = date("Y-m-d H:i:s");
81 2
        $this->save();
82 2
    }
83
84
85
86
    /**
87
     * Dynamicly set user properties to its value.
88
     *
89
     * @param User            $user user object
90
     */
91 3
    public function setUserData(User $user)
92
    {
93 3
        $userVarArray = get_object_vars($user);
94
95 3
        foreach ($userVarArray as $key => $value) {
96 3
            if ($value !== null) {
97 3
                $this->{$key} = $value;
98 3
            }
99 3
        }
100 3
    }
101
102
103
104
    /**
105
     * Dynamicly get user by field.
106
     *
107
     * @param  string          $field Fieldname to search.
108
     *
109
     * @param  mixed           $data Data to search for in the field.
110
     *
111
     * @return User            Returns a user.
112
     */
113 10
    public function getUserByField($field, $data)
114
    {
115 10
        return $this->find($field, $data);
116
    }
117
118
119
120
    /**
121
     * Returns all users stored and that are not deleted.
122
     *
123
     * @return array           Array with all users.
124
     */
125 2
    public function findAllUsers()
126
    {
127 2
        $this->checkDb();
128 2
        return $this->db->connect()
129 2
                        ->select()
130 2
                        ->from($this->tableName)
131 2
                        ->where("deleted IS NULL")
132 2
                        ->execute()
133 2
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
Documentation introduced by
get_class($this) is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
    }
135
}
136