Completed
Push — master ( 8e3af1...9c2c26 )
by satoru
01:52
created

normalFileDelete()   D

Complexity

Conditions 9
Paths 19

Size

Total Lines 40
Code Lines 25

Duplication

Lines 5
Ratio 12.5 %

Importance

Changes 0
Metric Value
dl 5
loc 40
rs 4.909
c 0
b 0
f 0
cc 9
eloc 25
nc 19
nop 3
1
<?php
2
3
namespace ContentsFile\Model\Behavior\Traits;
4
5
use Cake\Core\Configure;
6
use Cake\Filesystem\Folder;
7
use Cake\Network\Exception\InternalErrorException;
8
use Cake\ORM\TableRegistry;
9
10
/**
11
 * NormalContentsFileBehaviorTrait
12
 * 通常のファイルアップ系の処理
13
 * メソッド名の先頭に必ずnormalを付けること
14
 */
15
trait NormalContentsFileBehaviorTrait
16
{
17
18
    /**
19
     * normalParamCheck
20
     * 通常の設定値チェック
21
     * @author hagiwara
22
     */
23
    private function normalParamCheck()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
24
    {
25
        // S3に必要な設定がそろっているかチェックする
26
        $normalSetting = Configure::read('ContentsFile.Setting.Normal');
27
        if (
28
            !is_array($normalSetting) ||
29
            !array_key_exists('tmpDir', $normalSetting) ||
30
            !array_key_exists('fileDir', $normalSetting)
31
        ) {
32
            throw new InternalErrorException('contentsFileNormalConfig paramater shortage');
33
        }
34
        // /が最後についていない場合はつける
35
        if (!preg_match('#/$#', $normalSetting['tmpDir'])) {
36
            Configure::write('ContentsFile.Setting.Normal.tmpDir', $normalSetting['tmpDir'] . '/');
37
        }
38
        if (!preg_match('#/$#', $normalSetting['fileDir'])) {
39
            Configure::write('ContentsFile.Setting.Normal.fileDir', $normalSetting['fileDir'] . '/');
40
        }
41
    }
42
43
    /**
44
     * normalFileSave
45
     * ファイルを保存
46
     * @author hagiwara
47
     * @param array $fileInfo
48
     * @param array $fieldSettings
49
     * @param array $attachmentSaveData
50
     */
51
    private function normalFileSave($fileInfo, $fieldSettings, $attachmentSaveData)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
52
    {
53
        $newFiledir = Configure::read('ContentsFile.Setting.Normal.fileDir') . $attachmentSaveData['model'] . '/' . $attachmentSaveData['model_id'] . '/';
54
        // ランダムパスの場合の分岐
55 View Code Duplication
        if (Configure::read('ContentsFile.Setting.randomFile') === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            $newFilepath = $newFiledir . $attachmentSaveData['file_random_path'];
57
        } else {
58
            $newFilepath = $newFiledir . $fileInfo['field_name'];
59
        }
60
61
        if (Configure::read('ContentsFile.Setting.ext') === true) {
62
            $ext = (new \SplFileInfo($attachmentSaveData['file_name']))->getExtension();
63
            $newFilepath .= '.' . $ext;
64
        }
65
66
        //元ファイルは削除する
67
        $this->normalFileDelete($attachmentSaveData['model'], $attachmentSaveData['model_id'], $fileInfo['field_name']);
68
69
        if (
70
            !$this->mkdir($newFiledir, 0777, true) ||
0 ignored issues
show
Bug introduced by
It seems like mkdir() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
71
            !rename(Configure::read('ContentsFile.Setting.Normal.tmpDir') . $fileInfo['tmp_file_name'], $newFilepath)
72
        ) {
73
            return false;
74
        }
75
76
        //リサイズ画像作成
77 View Code Duplication
        if (!empty($fieldSettings['resize'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
            foreach ($fieldSettings['resize'] as $resizeSettings) {
79
                if (!$this->normalImageResize($newFilepath, $resizeSettings)) {
80
                    return false;
81
                }
82
            }
83
        }
84
        return true;
85
    }
86
87
    /**
88
     * normalFileDelete
89
     * 通常のファイル削除
90
     * @author hagiwara
91
     * @param string $modelName
92
     * @param integer $modelId
93
     * @param string $field
94
     */
95
    private function normalFileDelete($modelName, $modelId, $field)
96
    {
97
        //attachementからデータを取得
98
        $attachmentModel = TableRegistry::get('Attachments');
99
        $attachmentData = $attachmentModel->find('all')
100
            ->where(['model' => $modelName])
101
            ->where(['model_id' => $modelId])
102
            ->where(['field_name' => $field])
103
            ->first()
104
        ;
105
        // 削除するべきファイルがない
106
        if (empty($attachmentData)) {
107
            return false;
108
        }
109 View Code Duplication
        if (Configure::read('ContentsFile.Setting.randomFile') === true && $attachmentData->file_random_path != '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
            $deleteField = $attachmentData->file_random_path;
111
        } else {
112
            $deleteField = $attachmentData->field_name;
113
        }
114
115
        // リサイズのディレクトリ
116
        $resizeDir = Configure::read('ContentsFile.Setting.Normal.fileDir') . $modelName . '/' . $modelId . '/' . 'contents_file_resize_' . $deleteField . '/';
117
        if (is_dir($resizeDir)) {
118
            $deleteFolder = new Folder($resizeDir);
119
            if (!$deleteFolder->delete()) {
120
                return false;
121
            }
122
        }
123
124
        // 大元のファイル
125
        $deleteFile = Configure::read('ContentsFile.Setting.Normal.fileDir') . $modelName . '/' . $modelId . '/' . $deleteField;
126
        if (Configure::read('ContentsFile.Setting.ext') === true) {
127
            $ext = (new \SplFileInfo($attachmentData->file_name))->getExtension();
128
            $deleteFile .= '.' . $ext;
129
        }
130
        if (file_exists($deleteFile) && !unlink($deleteFile)) {
131
            return false;
132
        }
133
        return true;
134
    }
135
136
    /**
137
     * normalImageResize
138
     * 通常のファイルのリサイズ
139
     * @author hagiwara
140
     * @param string $newFilepath
141
     * @param array $resizeSettings
142
     */
143
    private function normalImageResize($newFilepath, $resizeSettings)
144
    {
145
        return $this->imageResize($newFilepath, $resizeSettings);
0 ignored issues
show
Bug introduced by
The method imageResize() does not exist on ContentsFile\Model\Behav...ntentsFileBehaviorTrait. Did you maybe mean normalImageResize()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
146
    }
147
}
148