Passed
Push — develop ( d1d076...95966b )
by Jens
03:19
created

UsersStorage   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 128
Duplicated Lines 21.88 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 28
loc 128
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getUsers() 0 4 1
A getUserBySlug() 14 14 3
B saveUser() 0 19 5
A addUser() 0 13 2
A deleteUserBySlug() 0 15 4
A getUserByUsername() 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
 * Created by jensk on 17-3-2017.
4
 */
5
6
namespace library\storage\storage;
7
8
9
use library\storage\factories\UserFactory;
10
11
class UsersStorage extends AbstractStorage
12
{
13
	/**
14
	 * Get all users
15
	 *
16
	 * @return mixed
17
	 */
18
	public function getUsers()
19
	{
20
		return $this->repository->users;
21
	}
22
23
	/**
24
	 * Get user by slug
25
	 *
26
	 * @param $slug
27
	 *
28
	 * @return array
29
	 */
30 View Code Duplication
	public function getUserBySlug($slug)
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...
31
	{
32
		$return = array();
33
34
		$users = $this->repository->users;
35
		foreach ($users as $user) {
36
			if ($user->slug == $slug) {
37
				$return = $user;
38
				break;
39
			}
40
		}
41
42
		return $return;
43
	}
44
45
	/**
46
	 * Save user
47
	 *
48
	 * @param $slug
49
	 * @param $postValues
50
	 *
51
	 * @throws \Exception
52
	 */
53
	public function saveUser($slug, $postValues)
54
	{
55
		$userObj = UserFactory::createUserFromPostValues($postValues);
56
		if ($userObj->slug != $slug) {
57
			// If the username changed, check for duplicates
58
			$doesItExist = $this->getUserBySlug($userObj->slug);
59
			if (!empty($doesItExist)) {
60
				throw new \Exception('Trying to rename user to existing username');
61
			}
62
		}
63
		$users = $this->getUsers();
64
		foreach ($users as $key => $user) {
65
			if ($user->slug == $slug) {
66
				$users[$key] = $userObj;
67
			}
68
		}
69
		$this->repository->users = $users;
70
		$this->save();
71
	}
72
73
	/**
74
	 * Add user
75
	 *
76
	 * @param $postValues
77
	 *
78
	 * @throws \Exception
79
	 */
80
	public function addUser($postValues)
81
	{
82
		$userObj = UserFactory::createUserFromPostValues($postValues);
83
84
		$doesItExist = $this->getUserBySlug($userObj->slug);
85
		if (!empty($doesItExist)) {
86
			throw new \Exception('Trying to add username that already exists.');
87
		}
88
		$users = $this->repository->users;
89
		$users[] = $userObj;
90
		$this->repository->users = $users;
91
		$this->save();
92
	}
93
94
	/**
95
	 * Delete user by slug
96
	 *
97
	 * @param $slug
98
	 *
99
	 * @throws \Exception
100
	 */
101
	public function deleteUserBySlug($slug)
102
	{
103
		$userToDelete = $this->getUserBySlug($slug);
104
		if (empty($userToDelete)) {
105
			throw new \Exception('Trying to delete a user that doesn\'t exist.');
106
		}
107
		$users = $this->getUsers();
108
		foreach ($users as $key => $user) {
109
			if ($user->slug == $userToDelete->slug) {
110
				unset($users[$key]);
111
				$this->repository->users = array_values($users);
112
			}
113
		}
114
		$this->save();
115
	}
116
117
	/**
118
	 * Get user by username
119
	 *
120
	 * @param $username
121
	 *
122
	 * @return array
123
	 */
124 View Code Duplication
	public function getUserByUsername($username)
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...
125
	{
126
		$return = array();
127
128
		$users = $this->repository->users;
129
		foreach ($users as $user) {
130
			if ($user->username == $username) {
131
				$return = $user;
132
				break;
133
			}
134
		}
135
136
		return $return;
137
	}
138
}