Completed
Push — master ( 504b57...819fbf )
by satoru
10s
created

normalParamCheck()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 5
nop 0
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
9
/**
10
 * NormalContentsFileBehaviorTrait
11
 * 通常のファイルアップ系の処理
12
 * メソッド名の先頭に必ずnormalを付けること
13
 */
14
trait NormalContentsFileBehaviorTrait
15
{
16
17
    /**
18
     * normalParamCheck
19
     * 通常の設定値チェック
20
     * @author hagiwara
21
     */
22
    private function normalParamCheck()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
23
    {
24
        // S3に必要な設定がそろっているかチェックする
25
        $normalSetting = Configure::read('ContentsFile.Setting.Normal');
26
        if (
27
            !is_array($normalSetting) ||
28
            !array_key_exists('tmpDir', $normalSetting) ||
29
            !array_key_exists('fileDir', $normalSetting)
30
        ) {
31
            throw new InternalErrorException('contentsFileNormalConfig paramater shortage');
32
        }
33
        // /が最後についていない場合はつける
34
        if (!preg_match('#/$#', $normalSetting['tmpDir'])) {
35
            Configure::write('ContentsFile.Setting.Normal.tmpDir', $normalSetting['tmpDir'] . '/');
36
        }
37
        if (!preg_match('#/$#', $normalSetting['fileDir'])) {
38
            Configure::write('ContentsFile.Setting.Normal.fileDir', $normalSetting['fileDir'] . '/');
39
        }
40
    }
41
42
    /**
43
     * normalFileSave
44
     * ファイルを保存
45
     * @author hagiwara
46
     * @param array $fileInfo
47
     * @param array $fieldSettings
48
     * @param array $attachmentSaveData
49
     */
50
    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...
51
    {
52
        $newFiledir = Configure::read('ContentsFile.Setting.Normal.fileDir') . $attachmentSaveData['model'] . '/' . $attachmentSaveData['model_id'] . '/';
53
        $newFilepath = $newFiledir . $fileInfo['field_name'];
54
        if (
55
            !$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...
56
            !rename(Configure::read('ContentsFile.Setting.Normal.tmpDir') . $fileInfo['tmp_file_name'], $newFilepath)
57
        ) {
58
            return false;
59
        }
60
61
        //リサイズディレクトリはまず削除する
62
        $Folder = new Folder($newFilepath);
63
        $Folder->delete();
64
65
        //リサイズ画像作成
66 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...
67
            foreach ($fieldSettings['resize'] as $resizeSettings) {
68
                if (!$this->normalImageResize($newFilepath, $resizeSettings)) {
69
                    return false;
70
                }
71
            }
72
        }
73
        return true;
74
    }
75
76
    /**
77
     * normalFileDelete
78
     * 通常のファイル削除
79
     * @author hagiwara
80
     * @param string $modelName
81
     * @param integer $modelId
82
     * @param string $field
83
     */
84
    private function normalFileDelete($modelName, $modelId, $field)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
85
    {
86
        // リサイズのディレクトリ
87
        $resizeDir = Configure::read('ContentsFile.Setting.Normal.fileDir') . $modelName . '/' . $modelId . '/' . 'contents_file_resize_' . $field . '/';
88
        if (is_dir($resizeDir)) {
89
            $deleteFolder = new Folder($resizeDir);
90
            if (!$deleteFolder->delete()) {
91
                return false;
92
            }
93
        }
94
95
        // 大元のファイル
96
        $deleteFile = Configure::read('ContentsFile.Setting.Normal.fileDir') . $modelName . '/' . $modelId . '/' . $field;
97
        if (file_exists($deleteFile) && !unlink($deleteFile)) {
98
            return false;
99
        }
100
        return true;
101
    }
102
103
    /**
104
     * normalImageResize
105
     * 通常のファイルのリサイズ
106
     * @author hagiwara
107
     * @param string $newFilepath
108
     * @param array $resizeSettings
109
     */
110
    private function normalImageResize($newFilepath, $resizeSettings)
111
    {
112
        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...
113
    }
114
}
115