Completed
Push — master ( 8284a5...a49521 )
by Martin
01:41
created

UserModel   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 181
Duplicated Lines 6.08 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 11
loc 181
ccs 51
cts 51
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B createUser() 0 25 3
A saveToSession() 0 14 1
A getUserFromDatabase() 0 9 1
A updateUserInDatabase() 0 20 3
A logoutUser() 0 13 1
A deleteUser() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 int $id the id of the user
110
     *
111
     * @return object $user the user object
112
     */
113 2
    public function getUserFromDatabase($id)
114
    {
115
        // Connect to db
116 2
        $user = new User();
117 2
        $user->setDb($this->di->get("db"));
118
        // Get the user
119 2
        $user->find("id", $id);
120 2
        return $user;
121
    }
122
123
124
    /**
125
     * Update a user in the database
126
     *
127
     * @param int $id the id of the user
128
     * @param object $update user object with new values
129
     *
130
     * @return object $user the user object
131
     */
132 1
    public function updateUserInDatabase($id, $update)
133
    {
134
        // Connect to db
135 1
        $user = new User();
136 1
        $user->setDb($this->di->get("db"));
137
        // Get the user
138 1
        $user->find("id", $id);
139
        // Update $user:
140 1
        $user->email = $update->email;
141 1
        if (isset($update->password)) {
142 1
            $user->setPassword($update->password);
143 1
        }
144 1
        $user->updated = date("Y-m-d H:i:s");
145 1
        if (isset($update->admin)) {
146 1
            $user->admin = $update->admin;
147 1
        }
148
        // Save to database
149 1
        $user->save();
150 1
        return $user;
151
    }
152
153
154
    /**
155
     * Logout user from session
156
     *
157
     * @return void
158
     */
159 1
    public function logoutUser()
160
    {
161
        // Unset session-key user
162 1
        $this->di->get("session")->delete("my_user_id");
163 1
        $this->di->get("session")->delete("my_user_name");
164
        //$this->di->get("session")->delete("my_user_password");
165 1
        $this->di->get("session")->delete("my_user_email");
166
        //$this->di->get("session")->delete("my_user_created");
167
        //$this->di->get("session")->delete("my_user_updated");
168
        //$this->di->get("session")->delete("my_user_deleted");
169
        //$this->di->get("session")->delete("my_user_active");
170 1
        $this->di->get("session")->delete("my_user_admin");
171 1
    }
172
173
174
    /**
175
     * Delete user
176
     * (Set $user->deleted to timestamp)
177
     *
178
     * @param int $id the id of the user to delete
179
     *
180
     * @return object $user the deleted useer
181
     */
182 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...
183
    {
184
        // Get user from db
185 1
        $user = new User();
186 1
        $user->setDb($this->di->get("db"));
187 1
        $user->find("id", $id);
188
        // Delete user (Set Deleted to timestamp)
189 1
        $user->deleted = date("Y-m-d H:i:s");
190 1
        $user->save();
191 1
        return $user;
192
    }
193
}
194