Complex classes like Updater often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Updater, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Updater extends RepositoryUpdater |
||
23 | { |
||
24 | use MessageQueuer; |
||
25 | |||
26 | /** |
||
27 | * @var Project\Issue |
||
28 | */ |
||
29 | protected $model; |
||
30 | |||
31 | public function __construct(Project\Issue $model) |
||
32 | { |
||
33 | $this->model = $model; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Set the issue is updated by a user. |
||
38 | * |
||
39 | * @return Project\Issue |
||
40 | */ |
||
41 | public function changeUpdatedBy() |
||
42 | { |
||
43 | $this->model->updated_by = $this->user->id; |
||
44 | $this->model->touch(); |
||
45 | |||
46 | return $this->save(); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Reassign the issue to a new user. |
||
51 | * |
||
52 | * @param int|User $assignTo |
||
53 | * @param User $user |
||
54 | * |
||
55 | * @return Project\Issue |
||
56 | */ |
||
57 | public function reassign($assignTo, User $user) |
||
58 | { |
||
59 | $this->setUser($user); |
||
60 | $assignToId = !$assignTo instanceof User ? $assignTo : $assignTo->id; |
||
61 | $this->model->assigned_to = $assignToId; |
||
62 | |||
63 | // Add event on successful save |
||
64 | Project\Issue::saved(function (Project\Issue $issue) { |
||
65 | $this->queueAssign($issue, $this->user); |
||
66 | }); |
||
67 | |||
68 | $this->save(); |
||
69 | |||
70 | $this->saveToActivities([ |
||
71 | 'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
||
72 | 'parent_id' => $this->model->project->id, |
||
73 | 'user_id' => $user->id, |
||
74 | 'action_id' => $this->model->assigned_to, |
||
75 | ]); |
||
76 | |||
77 | return $this->model; |
||
78 | } |
||
79 | |||
80 | protected function filterUpdateAttributes(array $input) |
||
81 | { |
||
82 | $fill = array_only($input, ['title', 'body', 'assigned_to']); |
||
83 | $fill['updated_by'] = $this->model->updatedBy->id; |
||
84 | $fill['lock_quote'] = (bool) isset($input['time_quote']['lock']); |
||
85 | |||
86 | // Only save quote if not locked or locked & user allowed to modify it |
||
87 | if (array_key_exists('time_quote', $input) && |
||
88 | (!$this->model->isQuoteLocked() || $this->getLoggedUser()->can('lockQuote', $this->model)) |
||
89 | ) { |
||
90 | $fill['time_quote'] = $input['time_quote']; |
||
91 | } |
||
92 | |||
93 | return $fill; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Update the given issue. |
||
98 | * |
||
99 | * @param array $input |
||
100 | * |
||
101 | * @return Project\Issue |
||
102 | */ |
||
103 | public function update(array $input = []) |
||
104 | { |
||
105 | $this->model->fill($this->filterUpdateAttributes($input)); |
||
106 | |||
107 | /* Add to activity log for assignment if changed */ |
||
108 | if ($this->model->isDirty('assigned_to') && $this->model->assigned_to > 0) { |
||
109 | $this->saveToActivities([ |
||
110 | 'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
||
111 | 'parent_id' => $this->model->project->id, |
||
112 | 'user_id' => $this->model->updatedBy->id, |
||
113 | 'action_id' => $this->model->assigned_to, |
||
114 | ]); |
||
115 | } |
||
116 | |||
117 | $this->syncTags($input, $this->model->tags()->with('parent')->get()); |
||
118 | |||
119 | // Add event on successful save |
||
120 | Project\Issue::saved(function (Project\Issue $issue) { |
||
121 | $this->queueUpdate($issue, $this->user); |
||
122 | }); |
||
123 | |||
124 | return $this->save(); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Create a new issue. |
||
129 | * |
||
130 | * @param array $input |
||
131 | * |
||
132 | * @return Project\Issue |
||
133 | */ |
||
134 | public function create(array $input) |
||
135 | { |
||
136 | if (array_key_exists('project_id', $input)) { |
||
137 | $this->model->setRelation('project', Project::find((int) $input['project_id'])); |
||
138 | } |
||
139 | |||
140 | $fill = [ |
||
141 | 'created_by' => $this->model->user->id, |
||
142 | 'project_id' => $this->model->project->id, |
||
143 | 'title' => $input['title'], |
||
144 | 'body' => $input['body'], |
||
145 | 'assigned_to' => (int) $this->model->project->default_assignee, |
||
146 | ]; |
||
147 | |||
148 | if ($this->model->user->isDeveloperOrMore()) { |
||
149 | $fill['assigned_to'] = array_get($input, 'assigned_to', $fill['assigned_to']); |
||
150 | $fill['time_quote'] = array_get($input, 'time_quote'); |
||
151 | } |
||
152 | |||
153 | $this->model->fill($fill)->save(); |
||
154 | |||
155 | // Add issue to messages queue |
||
156 | $this->queueAdd($this->model, $this->user); |
||
157 | |||
158 | /* Add to user's activity log */ |
||
159 | $this->saveToActivities([ |
||
160 | 'type_id' => Activity::TYPE_CREATE_ISSUE, |
||
161 | 'parent_id' => $this->model->project->id, |
||
162 | 'user_id' => $this->model->user->id, |
||
163 | ]); |
||
164 | |||
165 | /* Add attachments to issue */ |
||
166 | Project\Issue\Attachment::instance()->updater()->updateIssueToken($input['upload_token'], $this->model->user->id, $this->model->id); |
||
167 | |||
168 | // Add default tag to newly created issue |
||
169 | $defaultTag = app('tinyissue.settings')->getFirstStatusTagId(); |
||
170 | if ($defaultTag > 0 && empty($input['tag_status'])) { |
||
171 | $input['tag_status'] = $defaultTag; |
||
172 | } |
||
173 | |||
174 | $this->syncTags($input); |
||
175 | |||
176 | return $this->model; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Move the issue (comments & activities) to another project. |
||
181 | * |
||
182 | * @param int $projectId |
||
183 | * |
||
184 | * @return Project\Issue |
||
185 | */ |
||
186 | public function changeProject($projectId) |
||
187 | { |
||
188 | $this->model->project_id = $projectId; |
||
189 | $this->save(); |
||
190 | $comments = $this->model->comments()->get(); |
||
191 | foreach ($comments as $comment) { |
||
192 | $comment->project_id = $projectId; |
||
193 | $comment->save(); |
||
194 | } |
||
195 | |||
196 | $activities = $this->model->activities()->get(); |
||
197 | foreach ($activities as $activity) { |
||
198 | $activity->parent_id = $projectId; |
||
199 | $activity->save(); |
||
200 | } |
||
201 | |||
202 | return $this->model; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Delete an issue. |
||
207 | * |
||
208 | * @return bool |
||
209 | * |
||
210 | * @throws \Exception |
||
211 | */ |
||
212 | public function delete() |
||
216 | |||
217 | protected function deleteIssue() |
||
218 | { |
||
219 | // Delete issue related data |
||
220 | $this->deleteComments(); |
||
221 | $this->deleteAttachments(); |
||
222 | $this->deleteUserActivities(); |
||
223 | $this->deleteIssueTags(); |
||
228 | |||
229 | /** |
||
230 | * @return void |
||
231 | */ |
||
232 | protected function deleteComments() |
||
238 | |||
239 | /** |
||
240 | * @return void |
||
241 | */ |
||
242 | protected function deleteAttachments() |
||
248 | |||
249 | /** |
||
250 | * @return void |
||
251 | */ |
||
252 | protected function deleteIssueTags() |
||
256 | |||
257 | /** |
||
258 | * @return void |
||
259 | */ |
||
260 | protected function deleteUserActivities() |
||
266 | |||
267 | /** |
||
268 | * Change the status of an issue. |
||
269 | * |
||
270 | * @param int $status |
||
271 | * @param User $user |
||
272 | * |
||
273 | * @return Project\Issue |
||
274 | */ |
||
275 | public function changeStatus($status, User $user) |
||
303 | |||
304 | /** |
||
305 | * Sync the issue tags. |
||
306 | * |
||
307 | * @param array $input |
||
308 | * @param Collection $currentTags |
||
309 | * |
||
310 | * @return bool |
||
311 | */ |
||
312 | public function syncTags(array $input, Collection $currentTags = null) |
||
380 | |||
381 | /** |
||
382 | * Add tag to the issue & close issue if added tag is Closed. |
||
383 | * |
||
384 | * @param Tag $newTag |
||
385 | * @param Tag $oldTag |
||
386 | * |
||
387 | * @return Project\Issue |
||
388 | */ |
||
389 | public function changeKanbanTag(Tag $newTag, Tag $oldTag) |
||
425 | } |
||
426 |