UserModel::updateUserInDatabase()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.4285
cc 3
eloc 12
nc 4
nop 2
crap 3
1
<?php
2
3
namespace Mafd16\User;
4
5
use \Anax\Configure\ConfigureInterface;
6
use \Anax\Configure\ConfigureTrait;
7
use \Anax\DI\InjectionAwareInterface;
8
use \Anax\Di\InjectionAwareTrait;
9
10
/**
11
 * User model for Comment system.
12
 */
13
class UserModel implements
14
    ConfigureInterface,
15
    InjectionAwareInterface
16
{
17
    use ConfigureTrait,
18
        InjectionAwareTrait;
19
20
    /**
21
     * Variables
22
     */
23
    //private $session;
24
25
26
    /**
27
     * Create a new User.
28
     * The acronym is unique!
29
     *
30
     * @param object $newUser the values of the new user.
31
     *
32
     * $newUser = (object) [
33
     *      acronym => name,
34
     *      password => hashing in function!,
35
     *      email => ,
36
     *      created => timestamp in function,
37
     *      updated => null,
38
     *      deleted => null,
39
     *      active => null,
40
     *      admin => 0,
41
     *  ];
42
     *
43
     * @return object $user if acronym unique, false otherwise.
44
     */
45 4
    public function createUser($newUser)
46
    {
47
        // Connect to db
48 4
        $user = new User();
49 4
        $user->setDb($this->di->get("db"));
50
51
        // Check if acronym already exists,
52 4
        if ($user->find("acronym", $newUser->acronym)) {
53 1
            return false;
54
        } else {
55
            // else create user
56 4
            $user->acronym = $newUser->acronym;
57 4
            $user->setPassword($newUser->password);
58 4
            $user->email = $newUser->email;
59 4
            $user->created = date("Y-m-d H:i:s");
60
            //$user->$updated;
61
            //$user->$deleted;
62
            //$user->$active;
63 4
            $user->admin = isset($newUser->admin) ? $newUser->admin : 0;
64
            //$user->admin = 0;
65
            // Save to database
66 4
            $user->save();
67 4
            return $user;
68
        }
69
    }
70
71
72
    /**
73
     * Save a user to session. (Should maybe be called loginUser)
74
     *
75
     * @param object $user the values of the user.
76
     *
77
     * $user = (object) [
78
     *      acronym => name,
79
     *      password => hashed,
80
     *      email => email,
81
     *      created => timestamp,
82
     *      updated => null/timestamp,
83
     *      deleted => null/timestamp,
84
     *      active => null/timestamp,
85
     *      admin => 0/1,
86
     *  ];
87
     *
88
     * @return void
89
     */
90 2
    public function saveToSession($user)
91
    {
92 2
        $session = $this->di->get("session");
93
        // Save user to session
94 2
        $session->set("my_user_id", $user->id);
95 2
        $session->set("my_user_name", $user->acronym);
96
        //$session->set("my_user_password", $user->password);
97 2
        $session->set("my_user_email", $user->email);
98
        //$session->set("my_user_created", $user->created);
99
        //$session->set("my_user_updated", $user->updated);
100
        //$session->set("my_user_deleted", $user->deleted);
101
        //$session->set("my_user_active", $user->active);
102 2
        $session->set("my_user_admin", $user->admin);
103 2
    }
104
105
106
    /**
107
     * Get a user from the database
108
     *
109
     * @param string $key corresponding to a column in the table User
110
     * @param mixed $value the value of the key
111
     *
112
     * @return object $user the user object
113
     */
114 2
    public function getUserFromDatabase($key, $value)
115
    {
116
        // Connect to db
117 2
        $user = new User();
118 2
        $user->setDb($this->di->get("db"));
119
        // Get the user
120 2
        $user->find($key, $value);
121 2
        return $user;
122
    }
123
124
125
    /**
126
     * Update a user in the database
127
     *
128
     * @param int $id the id of the user
129
     * @param object $update user object with new values
130
     *
131
     * @return object $user the user object
132
     */
133 1
    public function updateUserInDatabase($id, $update)
134
    {
135
        // Connect to db
136 1
        $user = new User();
137 1
        $user->setDb($this->di->get("db"));
138
        // Get the user
139 1
        $user->find("id", $id);
140
        // Update $user:
141 1
        $user->email = $update->email;
142 1
        if (isset($update->password)) {
143 1
            $user->setPassword($update->password);
144 1
        }
145 1
        $user->updated = date("Y-m-d H:i:s");
146 1
        if (isset($update->admin)) {
147 1
            $user->admin = $update->admin;
148 1
        }
149
        // Save to database
150 1
        $user->save();
151 1
        return $user;
152
    }
153
154
155
    /**
156
     * Logout user from session
157
     *
158
     * @return void
159
     */
160 1
    public function logoutUser()
161
    {
162
        // Unset session-key user
163 1
        $this->di->get("session")->delete("my_user_id");
164 1
        $this->di->get("session")->delete("my_user_name");
165
        //$this->di->get("session")->delete("my_user_password");
166 1
        $this->di->get("session")->delete("my_user_email");
167
        //$this->di->get("session")->delete("my_user_created");
168
        //$this->di->get("session")->delete("my_user_updated");
169
        //$this->di->get("session")->delete("my_user_deleted");
170
        //$this->di->get("session")->delete("my_user_active");
171 1
        $this->di->get("session")->delete("my_user_admin");
172 1
    }
173
174
175
    /**
176
     * Delete user
177
     * (Set $user->deleted to timestamp)
178
     *
179
     * @param int $id the id of the user to delete
180
     *
181
     * @return object $user the deleted useer
182
     */
183 1 View Code Duplication
    public function deleteUser($id)
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...
184
    {
185
        // Get user from db
186 1
        $user = new User();
187 1
        $user->setDb($this->di->get("db"));
188 1
        $user->find("id", $id);
189
        // Delete user (Set Deleted to timestamp)
190 1
        $user->deleted = date("Y-m-d H:i:s");
191 1
        $user->save();
192 1
        return $user;
193
    }
194
}
195