| Conditions | 10 |
| Paths | 14 |
| Total Lines | 60 |
| Code Lines | 34 |
| 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 |
||
| 49 | public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) |
||
| 50 | { |
||
| 51 | //設定値をentityから取得 |
||
| 52 | $contentsFileConfig = $entity->getContentsFileSettings(); |
||
| 53 | $attachmentModel = TableRegistry::get('Attachments'); |
||
| 54 | foreach ($contentsFileConfig['fields'] as $field => $fieldSettings) { |
||
| 55 | // ファイルの削除を最初に確認 |
||
| 56 | if ($entity->{'delete_' . $field} == true) { |
||
| 57 | // 該当フィールドを削除 |
||
| 58 | if (!$this->fileDelete($entity, [$field])) { |
||
| 59 | return false; |
||
| 60 | } |
||
| 61 | // ファイルの削除に成功したら保存処理は飛ばす |
||
| 62 | continue; |
||
| 63 | } |
||
| 64 | //contents_file_の方に入ったentityをベースに処理する |
||
| 65 | $fileInfo = $entity->{'contents_file_' . $field}; |
||
| 66 | if ( |
||
| 67 | !empty($fileInfo) && |
||
| 68 | //tmp_file_nameがある=アップロードしたファイルがある |
||
| 69 | array_key_exists('tmp_file_name', $fileInfo) |
||
| 70 | ) { |
||
| 71 | // ファイルの削除 |
||
| 72 | $attachmentSaveData = [ |
||
| 73 | 'model' => $fileInfo['model'], |
||
| 74 | 'model_id' => $entity->id, |
||
| 75 | 'field_name' => $fileInfo['field_name'], |
||
| 76 | 'file_name' => $fileInfo['file_name'], |
||
| 77 | 'file_content_type' => $fileInfo['file_content_type'], |
||
| 78 | 'file_size' => $fileInfo['file_size'], |
||
| 79 | ]; |
||
| 80 | if (Configure::read('ContentsFile.Setting.randomFile') === true) { |
||
| 81 | $attachmentSaveData['file_random_path'] = $this->makeRandomPath(); |
||
| 82 | } |
||
| 83 | $attachmentEntity = $attachmentModel->newEntity($attachmentSaveData); |
||
| 84 | //元のデータがあるかfind(あれば元のファイルを消す) |
||
| 85 | $oldAttachmentData = $attachmentModel->find('all') |
||
| 86 | ->where(['model' => $fileInfo['model']]) |
||
| 87 | ->where(['model_id' => $entity->id]) |
||
| 88 | ->where(['field_name' => $fileInfo['field_name']]) |
||
| 89 | ->first(); |
||
| 90 | |||
| 91 | // 通常とS3で画像保存方法の切り替え |
||
| 92 | if (!$this->{Configure::read('ContentsFile.Setting.type') . 'FileSave'}($fileInfo, $fieldSettings, $attachmentSaveData, $oldAttachmentData)) { |
||
| 93 | return false; |
||
| 94 | } |
||
| 95 | |||
| 96 | //元のデータがあれば更新にする |
||
| 97 | if (!empty($oldAttachmentData)) { |
||
| 98 | $attachmentEntity->id = $oldAttachmentData->id; |
||
| 99 | } |
||
| 100 | if (!$attachmentModel->save($attachmentEntity)) { |
||
| 101 | return false; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | return true; |
||
| 107 | |||
| 108 | } |
||
| 109 | |||
| 229 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.