normalFileDelete()   B
last analyzed

Complexity

Conditions 9
Paths 19

Size

Total Lines 40

Duplication

Lines 5
Ratio 12.5 %

Importance

Changes 0
Metric Value
dl 5
loc 40
rs 7.7244
c 0
b 0
f 0
cc 9
nc 19
nop 3
1
<?php
2
3
namespace ContentsFile\Model\Behavior\Traits;
4
5
use Cake\Core\Configure;
6
use Cake\Network\Exception\InternalErrorException;
7
use Cake\ORM\TableRegistry;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
/**
11
 * NormalContentsFileBehaviorTrait
12
 * 通常のファイルアップ系の処理
13
 * メソッド名の先頭に必ずnormalを付けること
14
 */
15
trait NormalContentsFileBehaviorTrait
16
{
17
18
    /**
19
     * normalParamCheck
20
     * 通常の設定値チェック
21
     * @author hagiwara
22
     * @return void
23
     */
24
    private function normalParamCheck(): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
25
    {
26
        // S3に必要な設定がそろっているかチェックする
27
        $normalSetting = Configure::read('ContentsFile.Setting.Normal');
28 View Code Duplication
        if (
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...
29
            !is_array($normalSetting) ||
30
            !array_key_exists('tmpDir', $normalSetting) ||
31
            !array_key_exists('fileDir', $normalSetting)
32
        ) {
33
            throw new InternalErrorException('contentsFileNormalConfig paramater shortage');
34
        }
35
        // /が最後についていない場合はつける
36
        if (!preg_match('#/$#', $normalSetting['tmpDir'])) {
37
            Configure::write('ContentsFile.Setting.Normal.tmpDir', $normalSetting['tmpDir'] . '/');
38
        }
39
        if (!preg_match('#/$#', $normalSetting['fileDir'])) {
40
            Configure::write('ContentsFile.Setting.Normal.fileDir', $normalSetting['fileDir'] . '/');
41
        }
42
    }
43
44
    /**
45
     * normalFileSave
46
     * ファイルを保存
47
     * @author hagiwara
48
     * @param array $fileInfo
49
     * @param array $fieldSettings
50
     * @param array $attachmentSaveData
51
     * @return bool
52
     */
53
    private function normalFileSave(array $fileInfo, array $fieldSettings, array $attachmentSaveData): bool
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
54
    {
55
        $newFiledir = Configure::read('ContentsFile.Setting.Normal.fileDir') . $attachmentSaveData['model'] . '/' . $attachmentSaveData['model_id'] . '/';
56
        // ランダムパスの場合の分岐
57 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...
58
            $newFilepath = $newFiledir . $attachmentSaveData['file_random_path'];
59
        } else {
60
            $newFilepath = $newFiledir . $fileInfo['field_name'];
61
        }
62
63
        if (Configure::read('ContentsFile.Setting.ext') === true) {
64
            $ext = (new \SplFileInfo($attachmentSaveData['file_name']))->getExtension();
65
            $newFilepath .= '.' . $ext;
66
        }
67
68
        //元ファイルは削除する
69
        $this->normalFileDelete($attachmentSaveData['model'], $attachmentSaveData['model_id'], $fileInfo['field_name']);
70
71
        if (
72
            !$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...
73
            !rename(Configure::read('ContentsFile.Setting.Normal.tmpDir') . $fileInfo['tmp_file_name'], $newFilepath)
74
        ) {
75
            return false;
76
        }
77
78
        //リサイズ画像作成
79 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...
80
            foreach ($fieldSettings['resize'] as $resizeSettings) {
81
                if (!$this->normalImageResize($newFilepath, $resizeSettings)) {
82
                    return false;
83
                }
84
            }
85
        }
86
        return true;
87
    }
88
89
    /**
90
     * normalFileDelete
91
     * 通常のファイル削除
92
     * @author hagiwara
93
     * @param string $modelName
94
     * @param integer $modelId
95
     * @param string $field
96
     * @return bool
97
     */
98
    private function normalFileDelete(string $modelName, int $modelId, string $field): bool
99
    {
100
        //attachementからデータを取得
101
        $attachmentModel = TableRegistry::getTableLocator()->get('Attachments');
102
        $attachmentData = $attachmentModel->find('all')
103
            ->where(['model' => $modelName])
104
            ->where(['model_id' => $modelId])
105
            ->where(['field_name' => $field])
106
            ->first()
107
        ;
108
        // 削除するべきファイルがない
109
        if (empty($attachmentData)) {
110
            return false;
111
        }
112 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...
113
            $deleteField = $attachmentData->file_random_path;
114
        } else {
115
            $deleteField = $attachmentData->field_name;
116
        }
117
118
        // リサイズのディレクトリ
119
        $resizeDir = Configure::read('ContentsFile.Setting.Normal.fileDir') . $modelName . '/' . $modelId . '/' . 'contents_file_resize_' . $deleteField . '/';
120
        if (is_dir($resizeDir)) {
121
            $filesystem = new Filesystem();
122
            if (!$filesystem->remove($resizeDir)) {
123
                return false;
124
            }
125
        }
126
127
        // 大元のファイル
128
        $deleteFile = Configure::read('ContentsFile.Setting.Normal.fileDir') . $modelName . '/' . $modelId . '/' . $deleteField;
129
        if (Configure::read('ContentsFile.Setting.ext') === true) {
130
            $ext = (new \SplFileInfo($attachmentData->file_name))->getExtension();
131
            $deleteFile .= '.' . $ext;
132
        }
133
        if (file_exists($deleteFile) && !unlink($deleteFile)) {
134
            return false;
135
        }
136
        return true;
137
    }
138
139
    /**
140
     * normalImageResize
141
     * 通常のファイルのリサイズ
142
     * @author hagiwara
143
     * @param string $newFilepath
144
     * @param array $resizeSettings
145
     * @return bool
146
     */
147
    private function normalImageResize(string $newFilepath, array $resizeSettings): bool
148
    {
149
        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...
150
    }
151
}
152