Completed
Push — brickrouge3 ( 23d6f2...241af5 )
by Olivier
08:26
created

UserModel   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 76
Duplicated Lines 53.95 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 1
cbo 3
dl 41
loc 76
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A save_roles() 19 19 4
C save() 8 38 8
A save_restricted_sites() 14 14 3

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
/*
4
 * This file is part of the Icybee package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Icybee\Modules\Users;
13
14
use ICanBoogie\DateTime;
15
use ICanBoogie\ActiveRecord;
16
17
use Icybee\ConstructorModel;
18
19
class UserModel extends ConstructorModel
20
{
21
	public function save(array $properties, $key = null, array $options = [])
22
	{
23
		if (!$key)
24
		{
25 View Code Duplication
			if (empty($properties[User::PASSWORD_HASH]))
26
			{
27
				$properties[User::PASSWORD_HASH] = User::hash_password(uniqid(true));
28
			}
29
30
			if (empty($properties[User::CREATED_AT]) || DateTime::from($properties[User::CREATED_AT])->is_empty)
31
			{
32
				$properties[User::CREATED_AT] = DateTime::now();
33
			}
34
		}
35
36
		#
37
		# If defined, the password is encrypted before we pass it to our super class.
38
		#
39
40 View Code Duplication
		if (!empty($properties[User::PASSWORD]))
41
		{
42
			$properties[User::PASSWORD_HASH] = User::hash_password($properties[User::PASSWORD]);
43
		}
44
45
		$rc = parent::save($properties, $key, $options);
46
47
		if (array_key_exists(User::ROLES, $properties))
48
		{
49
			$this->save_roles($key, $rc, $properties[User::ROLES]);
50
		}
51
52
		if (array_key_exists(User::RESTRICTED_SITES, $properties))
53
		{
54
			$this->save_restricted_sites($key, $rc, $properties[User::RESTRICTED_SITES]);
55
		}
56
57
		return $rc;
58
	}
59
60 View Code Duplication
	protected function save_roles($key, $rc, $roles)
61
	{
62
		$has_many_roles = $this->models['users/has_many_roles'];
63
64
		if ($key)
65
		{
66
			$has_many_roles->filter_by_uid($key)->delete();
67
		}
68
69
		foreach ($roles as $rid)
70
		{
71
			if ($rid == 2)
72
			{
73
				continue;
74
			}
75
76
			$has_many_roles->execute('INSERT {self} SET uid = ?, rid = ?', [ $rc, $rid ]);
77
		}
78
	}
79
80 View Code Duplication
	protected function save_restricted_sites($key, $rc, $restricted_sites)
81
	{
82
		$has_many_sites = $this->models['users/has_many_sites'];
83
84
		if ($key)
85
		{
86
			$has_many_sites->filter_by_uid($key)->delete();
87
		}
88
89
		foreach ($restricted_sites as $site_id)
90
		{
91
			$has_many_sites->execute('INSERT {self} SET uid = ?, site_id = ?', [ $rc, $site_id ]);
92
		}
93
	}
94
}
95