Conditions | 13 |
Paths | 2 |
Total Lines | 112 |
Code Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
240 | protected function getEmitter() |
||
241 | { |
||
242 | static $emitter = null; |
||
243 | if ($emitter) { |
||
244 | return $emitter; |
||
245 | } |
||
246 | |||
247 | $emitter = new Emitter(); |
||
248 | $acl = $this->container->get('acl'); |
||
249 | $adapter = $this->container->get('connection'); |
||
250 | |||
251 | $emitter->addAction('table.insert.directus_groups', function ($data) use ($acl, $adapter) { |
||
252 | $privilegesTable = new DirectDirectusPrivilegesTableGateway($adapter, $acl); |
||
253 | |||
254 | $privilegesTable->insertPrivilege([ |
||
255 | 'group_id' => $data['id'], |
||
256 | 'allow_view' => 1, |
||
257 | 'allow_add' => 0, |
||
258 | 'allow_edit' => 1, |
||
259 | 'allow_delete' => 0, |
||
260 | 'allow_alter' => 0, |
||
261 | 'table_name' => 'directus_users', |
||
262 | 'read_field_blacklist' => 'token', |
||
263 | 'write_field_blacklist' => 'group,token' |
||
264 | ]); |
||
265 | }); |
||
266 | |||
267 | $emitter->addFilter('table.insert:before', function($payload) { |
||
268 | // $tableName, $data |
||
269 | if (func_num_args() == 2) { |
||
270 | $tableName = func_get_arg(0); |
||
271 | $data = func_get_arg(1); |
||
272 | } else { |
||
273 | $tableName = $payload->tableName; |
||
274 | $data = $payload->data; |
||
275 | } |
||
276 | |||
277 | if ($tableName == 'directus_files') { |
||
278 | unset($data['data']); |
||
279 | $data['user'] = 1; |
||
280 | } |
||
281 | |||
282 | return func_num_args() == 2 ? $data : $payload; |
||
283 | }); |
||
284 | |||
285 | // Add file url and thumb url |
||
286 | $config = $this->container->get('config'); |
||
287 | $files = $this->container->get('files'); |
||
288 | $emitter->addFilter('table.select', function($payload) use ($config, $files) { |
||
289 | if (func_num_args() == 2) { |
||
290 | $result = func_get_arg(0); |
||
291 | $selectState = func_get_arg(1); |
||
292 | } else { |
||
293 | $selectState = $payload->selectState; |
||
294 | $result = $payload->result; |
||
295 | } |
||
296 | |||
297 | if ($selectState['table'] == 'directus_files') { |
||
298 | $fileRows = $result->toArray(); |
||
299 | foreach ($fileRows as &$row) { |
||
300 | $fileURL = ArrayUtils::get($config, 'filesystem.root_url', ''); |
||
301 | $thumbnailURL = ArrayUtils::get($config, 'filesystem.root_thumb_url', ''); |
||
302 | $thumbnailFilenameParts = explode('.', $row['name']); |
||
303 | $thumbnailExtension = array_pop($thumbnailFilenameParts); |
||
304 | |||
305 | $row['url'] = $fileURL . '/' . $row['name']; |
||
306 | if (in_array($thumbnailExtension, ['tif', 'tiff', 'psd', 'pdf'])) { |
||
307 | $thumbnailExtension = 'jpg'; |
||
308 | } |
||
309 | |||
310 | $thumbnailFilename = $row['id'] . '.' . $thumbnailExtension; |
||
311 | $row['thumbnail_url'] = $thumbnailURL . '/' . $thumbnailFilename; |
||
312 | |||
313 | // filename-ext-100-100-true.jpg |
||
314 | // @TODO: This should be another hook listener |
||
315 | $row['thumbnail_url'] = null; |
||
316 | $filename = implode('.', $thumbnailFilenameParts); |
||
317 | if ($row['type'] == 'embed/vimeo') { |
||
318 | $oldThumbnailFilename = $row['name'] . '-vimeo-220-124-true.jpg'; |
||
319 | } else { |
||
320 | $oldThumbnailFilename = $filename . '-' . $thumbnailExtension . '-160-160-true.jpg'; |
||
321 | } |
||
322 | |||
323 | // 314551321-vimeo-220-124-true.jpg |
||
324 | // hotfix: there's not thumbnail for this file |
||
325 | if ($files->exists('thumbs/' . $oldThumbnailFilename)) { |
||
326 | $row['thumbnail_url'] = $thumbnailURL . '/' . $oldThumbnailFilename; |
||
327 | } |
||
328 | |||
329 | if ($files->exists('thumbs/' . $thumbnailFilename)) { |
||
330 | $row['thumbnail_url'] = $thumbnailURL . '/' . $thumbnailFilename; |
||
331 | } |
||
332 | |||
333 | /* |
||
334 | $embedManager = Bootstrap::get('embedManager'); |
||
335 | $provider = $embedManager->getByType($row['type']); |
||
336 | $row['html'] = null; |
||
337 | if ($provider) { |
||
338 | $row['html'] = $provider->getCode($row); |
||
339 | } |
||
340 | */ |
||
341 | } |
||
342 | |||
343 | $filesArrayObject = new \ArrayObject($fileRows); |
||
344 | $result->initialize($filesArrayObject->getIterator()); |
||
345 | } |
||
346 | |||
347 | return (func_num_args() == 2) ? $result : $payload; |
||
348 | }); |
||
349 | |||
350 | return $emitter; |
||
351 | } |
||
352 | |||
365 | } |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: