1 | <?php |
||
45 | trait CrudTagTrait |
||
46 | { |
||
47 | use DataMappingTrait, |
||
48 | FilterTrait; |
||
49 | |||
50 | /** |
||
51 | * Change the status of an issue. |
||
52 | * |
||
53 | * @param int $status |
||
54 | * @param int $userId |
||
55 | * |
||
56 | * @return Eloquent\Model |
||
57 | */ |
||
58 | 5 | public function changeStatus($status, $userId) |
|
59 | { |
||
60 | 5 | if ($status == 0) { |
|
61 | 5 | $this->closed_by = $userId; |
|
62 | 5 | $this->closed_at = (new \DateTime())->format('Y-m-d H:i:s'); |
|
63 | |||
64 | 5 | $activityType = Activity::TYPE_CLOSE_ISSUE; |
|
65 | 5 | $addTagName = Tag::STATUS_CLOSED; |
|
66 | |||
67 | /** @var \Illuminate\Support\Collection $ids */ |
||
68 | 5 | $ids = $this->getTagsExceptStatus()->getRelatedIds(); |
|
|
|||
69 | } else { |
||
70 | 1 | $activityType = Activity::TYPE_REOPEN_ISSUE; |
|
71 | 1 | $removeTag = Tag::STATUS_CLOSED; |
|
72 | 1 | $addTagName = Tag::STATUS_OPEN; |
|
73 | |||
74 | /** @var \Illuminate\Support\Collection $ids */ |
||
75 | 1 | $ids = $this->getTagsExcept($removeTag)->getRelatedIds(); |
|
76 | } |
||
77 | |||
78 | 5 | $ids->push((new Tag())->getTagByName($addTagName)->id); |
|
79 | |||
80 | 5 | $this->tags()->sync($ids->unique()->all()); |
|
81 | |||
82 | /* Add to activity log */ |
||
83 | 5 | $this->activities()->save(new User\Activity([ |
|
84 | 5 | 'type_id' => $activityType, |
|
85 | 5 | 'parent_id' => $this->project->id, |
|
86 | 5 | 'user_id' => $userId, |
|
87 | ])); |
||
88 | |||
89 | 5 | $this->status = $status; |
|
90 | |||
91 | 5 | return $this->save(); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Sync the issue tags. |
||
96 | * |
||
97 | * @param Collection $tags |
||
98 | * @param Collection $currentTags |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | 28 | public function syncTags(Collection $tags, Collection $currentTags = null) |
|
103 | { |
||
104 | 28 | $removedTags = []; |
|
105 | 28 | if (null === $currentTags) { |
|
106 | // Open status tag |
||
107 | 28 | $openTag = (new Tag())->getTagByName(Tag::STATUS_OPEN); |
|
108 | |||
109 | // Add the following tags except for open status |
||
110 | $addedTags = $tags |
||
111 | 28 | ->filter([$this, 'tagsExceptStatusOpenCallback']) |
|
112 | 28 | ->map([$this, 'toArrayCallback']) |
|
113 | 28 | ->toArray(); |
|
114 | } else { |
||
115 | // Open status tag |
||
116 | 1 | $openTag = $currentTags->first([$this, 'onlyStatusOpenCallback']); |
|
117 | |||
118 | // Remove status tag |
||
119 | 1 | $currentTags = $currentTags->filter([$this, 'tagsExceptStatusOpenCallback']); |
|
120 | |||
121 | // Make sure the tags does not includes the open status |
||
122 | 1 | $tags = $tags->filter([$this, 'tagsExceptStatusOpenCallback']); |
|
123 | |||
124 | // Tags remove from the issue |
||
125 | $removedTags = $currentTags |
||
126 | 1 | ->diff($tags) |
|
127 | 1 | ->map([$this, 'toArrayCallback']) |
|
128 | 1 | ->toArray(); |
|
129 | |||
130 | // Check if we are adding new tags |
||
131 | $addedTags = $tags |
||
132 | ->filter(function (Tag $tag) use ($currentTags) { |
||
133 | 1 | return $currentTags->where('id', $tag->id)->count() === 0; |
|
134 | 1 | }) |
|
135 | 1 | ->map([$this, 'toArrayCallback']) |
|
136 | 1 | ->toArray(); |
|
137 | |||
138 | // No new tags to add or remove |
||
139 | 1 | if (empty($removedTags) && empty($addedTags)) { |
|
140 | 1 | return true; |
|
141 | } |
||
142 | } |
||
143 | |||
144 | // Make sure open status exists |
||
145 | 28 | $tags->put($openTag->id, $openTag); |
|
146 | |||
147 | // Save relation |
||
148 | 28 | $this->tags()->sync($tags->lists('id')->all()); |
|
149 | |||
150 | // Activity is added when new issue create with tags or updated with tags excluding the open status tag |
||
151 | 28 | if (!empty($removedTags) || !empty($addedTags)) { |
|
152 | // Add to activity log for tags if changed |
||
153 | 4 | $this->activities()->save(new User\Activity([ |
|
154 | 4 | 'type_id' => Activity::TYPE_ISSUE_TAG, |
|
155 | 4 | 'parent_id' => $this->project->id, |
|
156 | 4 | 'user_id' => $this->user->id, |
|
157 | 4 | 'data' => ['added_tags' => $addedTags, 'removed_tags' => $removedTags], |
|
158 | ])); |
||
159 | } |
||
160 | |||
161 | 28 | return true; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Create new tags from a string "group:tag_name" and fetch tag from a tag id. |
||
166 | * |
||
167 | * @param array $tags |
||
168 | * @param bool $isAdmin |
||
169 | * |
||
170 | * @return Collection |
||
171 | */ |
||
172 | 28 | protected function createTags(array $tags, $isAdmin = false) |
|
192 | |||
193 | /** |
||
194 | * Add tag to the issue & close issue if added tag is Closed. |
||
195 | * |
||
196 | * @param Tag $newTag |
||
197 | * @param Tag $oldTag |
||
198 | * @param User $user |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function setCurrentTag(Tag $newTag, Tag $oldTag, User $user) |
||
214 | } |
||
215 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.