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\Project; |
13
|
|
|
|
14
|
|
|
use Illuminate\Support\Collection; |
15
|
|
|
use Tinyissue\Model\Project; |
16
|
|
|
use Tinyissue\Model\Tag; |
17
|
|
|
use Tinyissue\Model\User; |
18
|
|
|
use Tinyissue\Repository\RepositoryUpdater; |
19
|
|
|
|
20
|
|
|
class Updater extends RepositoryUpdater |
21
|
|
|
{ |
22
|
|
|
public function __construct(Project $model) |
23
|
|
|
{ |
24
|
|
|
$this->model = $model; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* removes a user from a project. |
29
|
|
|
* |
30
|
|
|
* @param int $userId |
31
|
|
|
* |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function unassignUser($userId) |
35
|
|
|
{ |
36
|
|
|
return $this->model->projectUsers()->where('user_id', '=', $userId)->delete(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Assign a user to a project. |
41
|
|
|
* |
42
|
|
|
* @param int $userId |
43
|
|
|
* @param int $roleId |
44
|
|
|
* |
45
|
|
|
* @return Project\User |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function assignUser($userId, $roleId = 0) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
if ($userId <= 0) { |
50
|
|
|
return false; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->model->projectUsers()->save(new Project\User([ |
54
|
|
|
'user_id' => $userId, |
55
|
|
|
'role_id' => $roleId, |
56
|
|
|
])); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function assignUsers(array $userIds) |
60
|
|
|
{ |
61
|
|
|
foreach ($userIds as $userId) { |
62
|
|
|
$this->assignUser($userId); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create a new project. |
68
|
|
|
* |
69
|
|
|
* @param array $input |
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function create(array $input = []) |
74
|
|
|
{ |
75
|
|
|
$this->model->fill($input)->save(); |
76
|
|
|
|
77
|
|
|
$this->saveKanbanTags(array_get($input, 'columns', [])); |
78
|
|
|
$this->assignUsers(array_get($input, 'user', [])); |
79
|
|
|
|
80
|
|
|
/* Assign selected users to the project */ |
81
|
|
|
// if (isset($input['user']) && count($input['user']) > 0) { |
82
|
|
|
// foreach ($input['user'] as $id) { |
83
|
|
|
// $this->assignUser($id); |
84
|
|
|
// } |
85
|
|
|
// } |
86
|
|
|
|
87
|
|
|
return $this; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Update project details. |
92
|
|
|
* |
93
|
|
|
* @param array $attributes |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function update(array $attributes = []) |
98
|
|
|
{ |
99
|
|
|
$this->saveKanbanTags(array_get($attributes, 'columns', [])); |
100
|
|
|
|
101
|
|
|
return $this->model->update($attributes); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Delete a project. |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
* |
109
|
|
|
* @throws \Exception |
110
|
|
|
*/ |
111
|
|
|
public function delete() |
112
|
|
|
{ |
113
|
|
|
return $this->transaction('deleteProject'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function deleteProject() |
117
|
|
|
{ |
118
|
|
|
// Remove issues |
119
|
|
|
$issues = $this->model->issues()->get(); |
120
|
|
|
foreach ($issues as $issue) { |
121
|
|
|
$issue->delete(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Remove project notes |
125
|
|
|
$notes = $this->model->notes()->get(); |
126
|
|
|
foreach ($notes as $note) { |
127
|
|
|
$note->delete(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// Remove project users |
131
|
|
|
Project\User::where('project_id', '=', $this->model->id)->delete(); |
132
|
|
|
|
133
|
|
|
// Remove user activities |
134
|
|
|
User\Activity::where('parent_id', '=', $this->model->id)->delete(); |
135
|
|
|
|
136
|
|
|
// Remove kanban tags |
137
|
|
|
\DB::table('projects_kanban_tags')->where('project_id', '=', $this->model->id)->delete(); |
138
|
|
|
|
139
|
|
|
// Remove the project |
140
|
|
|
$this->removeProjectStorage($this->model); |
|
|
|
|
141
|
|
|
|
142
|
|
|
return $this->model->delete(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Save the project tags. |
147
|
|
|
* |
148
|
|
|
* @param array $tagIds |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
protected function saveKanbanTags(array $tagIds) |
153
|
|
|
{ |
154
|
|
|
if (empty($tagIds)) { |
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Transform the user input tags into tag objects |
159
|
|
|
// Filter out invalid tags entered by the user |
160
|
|
|
$tags = new Collection($tagIds); |
161
|
|
|
$tags = $tags->transform(function ($tagNameOrId) { |
162
|
|
|
return Tag::find($tagNameOrId); |
163
|
|
|
})->filter(function ($tag) { |
164
|
|
|
return $tag instanceof Tag; |
165
|
|
|
}); |
166
|
|
|
|
167
|
|
|
// Delete all existing |
168
|
|
|
$this->model->kanbanTags()->detach(); |
169
|
|
|
|
170
|
|
|
// Save tags |
171
|
|
|
$kanbanTags = $this->model->kanbanTags(); |
172
|
|
|
foreach ($tags as $position => $tag) { |
173
|
|
|
$kanbanTags->attach([$tag->id => ['position' => $position]]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return true; |
177
|
|
|
} |
178
|
|
|
// |
179
|
|
|
// protected function createNote() |
180
|
|
|
// { |
181
|
|
|
// $this->model->notes()->create() |
182
|
|
|
// } |
183
|
|
|
} |
184
|
|
|
|
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.