| Conditions | 4 |
| Paths | 4 |
| Total Lines | 76 |
| Code Lines | 54 |
| 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 |
||
| 15 | function transfer_file_to_new_user($object, $NU_guid, $OLD_guid) { |
||
| 16 | |||
| 17 | $prefix = 'file/'; |
||
| 18 | |||
| 19 | $file = new FilePluginFile($object->guid); |
||
| 20 | |||
| 21 | $nfh = new ElggFile(); |
||
| 22 | $nfh->owner_guid = $NU_guid; |
||
| 23 | if($file->container_guid != $OLD_guid){ |
||
| 24 | $nfh->container_guid = $file->container_guid; |
||
| 25 | } else { |
||
| 26 | $nfh->container_guid = $NU_guid; |
||
| 27 | } |
||
| 28 | $nfh->subtype = 'file'; |
||
| 29 | $nfh->title = $file->title; |
||
| 30 | $nfh->description = $file->description; |
||
| 31 | $nfh->access_id = $file->access_id; |
||
| 32 | $nfh->tags = $file->tags; |
||
| 33 | |||
| 34 | $filename = $file->getFilenameOnFilestore(); |
||
| 35 | |||
| 36 | $name = end(explode('/', $filename)); |
||
|
|
|||
| 37 | $nfh->setFilename($prefix . $name); |
||
| 38 | |||
| 39 | $file->open("read"); |
||
| 40 | $nfh->open("write"); |
||
| 41 | |||
| 42 | $nfh->write($file->grabFile()); |
||
| 43 | |||
| 44 | if($file->simpletype == "image"){ |
||
| 45 | //lets create some thumbnails |
||
| 46 | $thumb = new ElggFile(); |
||
| 47 | $thumb->owner_guid = $nfh->owner_guid; |
||
| 48 | |||
| 49 | $sizes = [ |
||
| 50 | 'small' => [ |
||
| 51 | 'w' => 60, |
||
| 52 | 'h' => 60, |
||
| 53 | 'square' => true, |
||
| 54 | 'metadata_name' => 'thumbnail', |
||
| 55 | 'filename_prefix' => 'thumb', |
||
| 56 | ], |
||
| 57 | 'medium' => [ |
||
| 58 | 'w' => 153, |
||
| 59 | 'h' => 153, |
||
| 60 | 'square' => true, |
||
| 61 | 'metadata_name' => 'smallthumb', |
||
| 62 | 'filename_prefix' => 'smallthumb', |
||
| 63 | ], |
||
| 64 | 'large' => [ |
||
| 65 | 'w' => 600, |
||
| 66 | 'h' => 600, |
||
| 67 | 'square' => false, |
||
| 68 | 'metadata_name' => 'largethumb', |
||
| 69 | 'filename_prefix' => 'largethumb', |
||
| 70 | ], |
||
| 71 | ]; |
||
| 72 | |||
| 73 | foreach ($sizes as $size => $data) { |
||
| 74 | $image_bytes = get_resized_image_from_existing_file($nfh->getFilenameOnFilestore(), $data['w'], $data['h'], $data['square']); |
||
| 75 | $name = explode('.', $name); |
||
| 76 | $filename2 = "{$prefix}{$data['filename_prefix']}{$name[0]}.jpg"; |
||
| 77 | $thumb->setFilename($filename2); |
||
| 78 | $thumb->open("write"); |
||
| 79 | $thumb->write($image_bytes); |
||
| 80 | $thumb->close(); |
||
| 81 | unset($image_bytes); |
||
| 82 | |||
| 83 | $nfh->{$data['metadata_name']} = $filename2; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | // close file |
||
| 88 | $file->close(); |
||
| 89 | $nfh->close(); |
||
| 90 | } |
||
| 91 |