These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /** |
||
6 | * balloon |
||
7 | * |
||
8 | * @copyright Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com) |
||
9 | * @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
||
10 | */ |
||
11 | |||
12 | namespace Balloon\Hook; |
||
13 | |||
14 | use Balloon\Filesystem\Node\Collection; |
||
15 | use Balloon\Filesystem\Node\File; |
||
16 | use Balloon\Filesystem\Node\NodeInterface; |
||
17 | |||
18 | class Delta extends AbstractHook |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | public function postCreateCollection(Collection $parent, Collection $node, bool $clone): void |
||
24 | { |
||
25 | if (true === $clone) { |
||
26 | return; |
||
27 | } |
||
28 | |||
29 | if ($node->isReference()) { |
||
30 | $operation = 'addCollectionReference'; |
||
31 | } else { |
||
32 | $operation = 'addCollection'; |
||
33 | } |
||
34 | |||
35 | $event = [ |
||
36 | 'parent' => $parent->getRealId(), |
||
37 | ]; |
||
38 | |||
39 | $parent->getFilesystem()->getDelta()->add($operation, $node, $event); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public function postCopyCollection( |
||
46 | Collection $node, |
||
47 | Collection $parent, |
||
48 | Collection $new_node, |
||
49 | int $conflict, |
||
50 | ?string $recursion, |
||
51 | bool $recursion_first |
||
52 | ): void { |
||
53 | $event = [ |
||
54 | 'parent' => $parent->getRealId(), |
||
55 | ]; |
||
56 | |||
57 | $parent->getFilesystem()->getDelta()->add('copyCollection', $new_node, $event); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function postCopyFile( |
||
64 | File $node, |
||
65 | Collection $parent, |
||
66 | File $new_node, |
||
67 | int $conflict, |
||
68 | ?string $recursion, |
||
69 | bool $recursion_first |
||
70 | ): void { |
||
71 | $event = [ |
||
72 | 'parent' => $parent->getRealId(), |
||
73 | ]; |
||
74 | |||
75 | $parent->getFilesystem()->getDelta()->add('copyFile', $new_node, $event); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function postCreateFile(Collection $parent, File $node, bool $clone): void |
||
82 | { |
||
83 | if (true === $clone) { |
||
84 | return; |
||
85 | } |
||
86 | |||
87 | $event = [ |
||
88 | 'parent' => $parent->getRealId(), |
||
89 | ]; |
||
90 | |||
91 | $parent->getFilesystem()->getDelta()->add('addFile', $node, $event); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | public function postDeleteCollection(Collection $node, bool $force, ?string $recursion, bool $recursion_first): void |
||
98 | { |
||
99 | if (false === $recursion_first) { |
||
100 | return; |
||
101 | } |
||
102 | |||
103 | if ($node->isReference()) { |
||
104 | if (true === $force) { |
||
105 | $operation = 'forceDeleteCollectionReference'; |
||
106 | } else { |
||
107 | $operation = 'deleteCollectionReference'; |
||
108 | } |
||
109 | } else { |
||
110 | if (true === $force) { |
||
111 | $operation = 'forceDeleteCollection'; |
||
112 | } else { |
||
113 | $operation = 'deleteCollection'; |
||
114 | } |
||
115 | } |
||
116 | |||
117 | $event['force'] = $force; |
||
118 | $event['parent'] = $node->getAttributes()['parent']; |
||
119 | $node->getFilesystem()->getDelta()->add($operation, $node, $event); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function postDeleteFile(File $node, bool $force, ?string $recursion, bool $recursion_first): void |
||
126 | { |
||
127 | if (false === $recursion_first) { |
||
128 | return; |
||
129 | } |
||
130 | |||
131 | if (true === $force) { |
||
132 | $operation = 'forceDeleteFile'; |
||
133 | } else { |
||
134 | $operation = 'deleteFile'; |
||
135 | } |
||
136 | |||
137 | $event['parent'] = $node->getAttributes()['parent']; |
||
138 | $event['force'] = $force; |
||
139 | $node->getFilesystem()->getDelta()->add($operation, $node, $event); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function postSaveNodeAttributes(NodeInterface $node, array $attributes, array $remove, ?string $recursion, bool $recursion_first): void |
||
146 | { |
||
147 | if (false === $recursion_first) { |
||
148 | return; |
||
149 | } |
||
150 | |||
151 | $raw = $node->getRawAttributes(); |
||
152 | $log = [ |
||
153 | 'parent' => $node->getParent()->getRealId(), |
||
154 | ]; |
||
155 | |||
156 | if ($node instanceof Collection) { |
||
157 | $suffix = 'Collection'; |
||
158 | } else { |
||
159 | $suffix = 'File'; |
||
160 | } |
||
161 | |||
162 | if ($node->isReference()) { |
||
163 | $suffix2 = 'Reference'; |
||
164 | } elseif ($node->isShare()) { |
||
165 | $suffix2 = 'Share'; |
||
166 | } else { |
||
167 | $suffix2 = ''; |
||
168 | } |
||
169 | |||
170 | if (in_array('shared', $attributes, true) && !$node->isShared() && array_key_exists('shared', $raw) && true === $raw['shared']) { |
||
171 | $operation = 'unshareCollection'; |
||
172 | } elseif (in_array('parent', $attributes, true) && $raw['parent'] !== $node->getAttributes()['parent']) { |
||
173 | $operation = 'move'.$suffix.$suffix2; |
||
174 | $log['previous'] = [ |
||
175 | 'parent' => $raw['parent'], |
||
176 | ]; |
||
177 | } elseif (in_array('name', $attributes, true) && $raw['name'] !== $node->getName()) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
178 | $operation = 'rename'.$suffix.$suffix2; |
||
179 | $log['previous'] = [ |
||
180 | 'name' => $raw['name'], |
||
181 | ]; |
||
182 | } elseif (in_array('deleted', $attributes, true) && $raw['deleted'] !== $node->getAttributes()['deleted'] |
||
183 | && !$node->isDeleted()) { |
||
184 | $operation = 'undelete'.$suffix.$suffix2; |
||
185 | } elseif (in_array('shared', $attributes, true) && $raw['shared'] !== $node->isShare() && $node->isShare()) { |
||
186 | $operation = 'add'.$suffix.$suffix2; |
||
187 | |||
188 | if ('addCollectionShare' === $operation) { |
||
189 | $this->updateExistingDeltaShareMember($node); |
||
190 | } |
||
191 | } elseif (in_array('shared', $attributes, true) && $raw['shared'] !== $node->isShare() && !$node->isShare()) { |
||
192 | $operation = 'delete'.$suffix.$suffix2; |
||
193 | } elseif ($node instanceof File && $node->getVersion() !== $raw['version'] && $raw['version'] !== 0) { |
||
194 | $history = $node->getHistory(); |
||
195 | $last = end($history); |
||
196 | |||
197 | if ($last['version'] === $node->getVersion()) { |
||
198 | switch ($last['type']) { |
||
199 | case File::HISTORY_EDIT: |
||
200 | $operation = 'editFile'; |
||
201 | $log['previous'] = [ |
||
202 | 'version' => $raw['version'], |
||
203 | ]; |
||
204 | |||
205 | break; |
||
206 | case File::HISTORY_RESTORE: |
||
207 | $operation = 'restoreFile'; |
||
208 | $log['previous'] = [ |
||
209 | 'version' => $raw['version'], |
||
210 | ]; |
||
211 | |||
212 | break; |
||
213 | } |
||
214 | } else { |
||
215 | $operation = 'editFile'; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | if (isset($operation)) { |
||
220 | $node->getFilesystem()->getDelta()->add($operation, $node, $log); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Update share delta entries. |
||
226 | */ |
||
227 | protected function updateExistingDeltaShareMember(NodeInterface $node): bool |
||
228 | { |
||
229 | $toset = $node->getChildrenRecursive($node->getRealId()); |
||
230 | $action = [ |
||
231 | '$set' => [ |
||
232 | 'share' => $node->getRealId(), |
||
233 | ], |
||
234 | ]; |
||
235 | |||
236 | $node->getFilesystem()->getDatabase()->delta->updateMany([ |
||
237 | 'node' => [ |
||
238 | '$in' => $toset, |
||
239 | ], |
||
240 | ], $action); |
||
241 | |||
242 | return true; |
||
243 | } |
||
244 | } |
||
245 |