|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Tinyissue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[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 Tinyissue\Repository\User; |
|
13
|
|
|
|
|
14
|
|
|
use Hash; |
|
15
|
|
|
use Tinyissue\Contracts\Model\UserInterface; |
|
16
|
|
|
use Tinyissue\Model\Project\User as ProjectUser; |
|
17
|
|
|
use Tinyissue\Model\User; |
|
18
|
|
|
use Tinyissue\Repository\RepositoryUpdater; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Updater. |
|
22
|
|
|
* |
|
23
|
|
|
* @package Tinyissue\\User |
|
24
|
|
|
*/ |
|
25
|
|
|
class Updater extends RepositoryUpdater |
|
26
|
|
|
{ |
|
27
|
|
|
protected $model; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(UserInterface $model) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->model = $model; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Add a new user. |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $info |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
View Code Duplication |
public function create(array $info) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$insert = [ |
|
44
|
|
|
'email' => $info['email'], |
|
45
|
|
|
'firstname' => $info['firstname'], |
|
46
|
|
|
'lastname' => $info['lastname'], |
|
47
|
|
|
'role_id' => $info['role_id'], |
|
48
|
|
|
'private' => (boolean) $info['private'], |
|
49
|
|
|
'password' => Hash::make($info['password']), |
|
50
|
|
|
'status' => $info['status'], |
|
51
|
|
|
'language' => app('tinyissue.settings')->getLanguage(), |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
return $this->model->fill($insert)->save(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Soft deletes a user and empties the email. |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public function delete() |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$this->model->update([ |
|
65
|
|
|
'email' => $this->model->email . '_deleted', |
|
|
|
|
|
|
66
|
|
|
'deleted' => User::DELETED_USERS, |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
ProjectUser::where('user_id', '=', $this->model->id)->delete(); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Updates the users settings, validates the fields. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $info |
|
|
|
|
|
|
78
|
|
|
* |
|
79
|
|
|
* @return Eloquent\Model |
|
80
|
|
|
*/ |
|
81
|
|
View Code Duplication |
public function update(array $attributes = []) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
if ($attributes['password']) { |
|
84
|
|
|
$attributes['password'] = Hash::make($attributes['password']); |
|
85
|
|
|
} elseif (empty($attributes['password'])) { |
|
86
|
|
|
unset($attributes['password']); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->model->update($attributes); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Update user messages setting. |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $input |
|
96
|
|
|
*/ |
|
97
|
|
View Code Duplication |
public function updateMessagesSettings(array $input) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
return (new ProjectUser()) |
|
|
|
|
|
|
100
|
|
|
->where('user_id', '=', $this->model->id) |
|
|
|
|
|
|
101
|
|
|
->whereIn('project_id', array_keys($input)) |
|
102
|
|
|
->get() |
|
103
|
|
|
->each(function (ProjectUser $project) use ($input) { |
|
104
|
|
|
$project->message_id = $input[$project->project_id]; |
|
105
|
|
|
$project->save(); |
|
106
|
|
|
}); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
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.