| Conditions | 18 |
| Paths | 174 |
| Total Lines | 116 |
| Code Lines | 69 |
| 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 |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | return true; |
||
| 101 | |||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * fileDelete |
||
| 106 | * ファイル削除 |
||
| 107 | * @author hagiwara |
||
| 108 | * @param EntityInterface $entity |
||
| 109 | * @param array $fields |
||
| 110 | */ |
||
| 111 | public function fileDelete(EntityInterface $entity, $fields = []) |
||
| 112 | { |
||
| 113 | // 新規作成データ時は何もしない |
||
| 114 | if (empty($entity->id)) { |
||
| 115 | return true; |
||
| 116 | } |
||
| 117 | $contentsFileConfig = $entity->getContentsFileSettings(); |
||
| 118 | if (!empty($contentsFileConfig['fields'])) { |
||
| 119 | foreach ($contentsFileConfig['fields'] as $field => $config) { |
||
| 120 | // fieldsの指定がない場合は全部消す |
||
| 121 | if (!empty($fields) && !in_array($field, $fields)) { |
||
| 122 | continue; |
||
| 123 | } |
||
| 124 | if (!$this->fileDeleteParts($entity, $field)) { |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | return true; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * fileDeleteParts |
||
| 134 | * ファイル削除 |
||
| 135 | * @author hagiwara |
||
| 136 | * @param EntityInterface $entity |
||
| 137 | * @param string $field |
||
| 138 | */ |
||
| 139 | private function fileDeleteParts($entity, $field) |
||
| 140 | { |
||
| 141 | $attachmentModel = TableRegistry::get('Attachments'); |
||
| 142 | $modelName = $entity->source(); |
||
| 143 | $modelId = $entity->id; |
||
| 144 | // 添付ファイルデータの削除 |
||
| 145 | $deleteAttachmentData = $attachmentModel->find('all') |
||
| 146 | ->where(['Attachments.model' => $modelName]) |
||
| 147 | ->where(['Attachments.model_id' => $modelId]) |
||
| 148 | ->where(['Attachments.field_name' => $field]) |
||
| 149 | ->first(); |
||
| 150 | |||
| 151 | if (!empty($deleteAttachmentData->id)) { |
||
| 152 | $attachmentModel->delete($deleteAttachmentData); |
||
| 153 | // 通常とS3でファイルの削除方法の切り替え |
||
| 154 | if (!$this->{Configure::read('ContentsFile.Setting.type') . 'FileDelete'}($modelName, $modelId, $field)) { |
||
| 155 | return false; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | return true; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * mkdir |
||
| 163 | * ディレクトリの作成(パーミッションの設定のため |
||
| 164 | * @author hagiwara |
||
| 165 | * @param string $permission |
||
| 166 | * @param integer $path |
||
| 167 | * @param boolean $recursive |
||
| 168 | */ |
||
| 169 | private function mkdir($path, $permission, $recursive) |
||
| 170 | { |
||
| 171 | if (is_dir($path)) { |
||
| 172 | return true; |
||
| 173 | } |
||
| 174 | $oldumask = umask(0); |
||
| 175 | $result = mkdir($path, $permission, $recursive); |
||
| 176 | umask($oldumask); |
||
| 177 | return $result; |
||
| 178 | } |
||
| 179 | } |
||
| 180 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.