Completed
Push — master ( 2825b9...504b57 )
by satoru
07:25 queued 05:25
created

S3ContentsFileBehaviorTrait::s3ParamCheck()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 12
Ratio 70.59 %

Importance

Changes 0
Metric Value
dl 12
loc 17
rs 7.7777
c 0
b 0
f 0
cc 8
eloc 11
nc 2
nop 0
1
<?php
2
3
namespace ContentsFile\Model\Behavior\Traits;
4
5
use ContentsFile\Aws\S3;
6
use Cake\Core\Configure;
7
use Cake\Filesystem\Folder;
8
use Cake\I18n\Time;
9
use Cake\Network\Exception\InternalErrorException;
10
use Cake\Utility\Security;
11
12
/**
13
 * S3ContentsFileBehaviorTrait
14
 * 通常のファイルアップ系の処理
15
 * メソッド名の先頭に必ずs4を付けること
16
 */
17
trait S3ContentsFileBehaviorTrait
18
{
19
20
    /**
21
     * s3ParamCheck
22
     * 通常の設定値チェック
23
     * @author hagiwara
24
     */
25
    private function s3ParamCheck()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
26
    {
27
        // S3に必要な設定がそろっているかチェックする
28
        $s3Setting = Configure::read('ContentsFile.Setting.S3');
29 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...
30
            !is_array($s3Setting) ||
31
            !array_key_exists('key', $s3Setting) ||
32
            !array_key_exists('secret', $s3Setting) ||
33
            !array_key_exists('bucket', $s3Setting) ||
34
            !array_key_exists('tmpDir', $s3Setting) ||
35
            !array_key_exists('fileDir', $s3Setting) ||
36
            !array_key_exists('workingDir', $s3Setting)
37
38
        ) {
39
            throw new InternalErrorException('contentsFileS3Config paramater shortage');
40
        }
41
    }
42
43
    /**
44
     * s3FileSave
45
     * ファイルをS3に保存
46
     * @author hagiwara
47
     * @param array $fileInfo
48
     * @param array $fieldSettings
49
     * @param array $attachmentSaveData
50
     */
51
    private function s3FileSave($fileInfo, $fieldSettings, $attachmentSaveData)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
52
    {
53
        $S3 = new S3();
54
        $newFiledir = Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $attachmentSaveData['model'] . '/' . $attachmentSaveData['model_id'] . '/';
55
        $newFilepath = $newFiledir . $fileInfo['field_name'];
56
        $oldFilepath = Configure::read('ContentsFile.Setting.S3.tmpDir') . '/' . $fileInfo['tmp_file_name'];
57
58
        // tmpに挙がっているファイルを移
59
        if (!$S3->move($oldFilepath, $newFilepath)) {
60
            return false;
61
        }
62
63
        // リサイズディレクトリはまず削除する
64
        // 失敗=ディレクトリが存在しないため、成功失敗判定は行わない。
65
        $S3->deleteRecursive($newFilepath . '/' . 'contents_file_resize_' . $fileInfo['field_name']);
66
67
        //リサイズ画像作成
68 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...
69
            foreach ($fieldSettings['resize'] as $resizeSettings) {
70
                if (!$this->s3ImageResize($newFilepath, $resizeSettings)) {
71
                    return false;
72
                }
73
            }
74
        }
75
        return true;
76
    }
77
78
    /**
79
     * s3FileDelete
80
     * S3のファイル削除
81
     * @author hagiwara
82
     * @param string $modelName
83
     * @param integer $modelId
84
     * @param string $field
85
     */
86
    private function s3FileDelete($modelName, $modelId, $field)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
87
    {
88
        $S3 = new S3();
89
        // リサイズのディレクトリ
90
        $resizeDir = Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $modelName . '/' . $modelId . '/' . 'contents_file_resize_' . $field . '/';
91
        if (!$S3->deleteRecursive($resizeDir)) {
92
            return false;
93
        }
94
95
        // 大元のファイル
96
        $deleteFile = Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $modelName . '/' . $modelId . '/' . $field;
97
        if (!$S3->delete($deleteFile)) {
98
            return false;
99
        }
100
        return true;
101
    }
102
103
    /**
104
     * s3ImageResize
105
     * 画像のリサイズ処理(S3用)
106
     * @author hagiwara
107
     * @param string $filepath
108
     * @param array $resize
109
     */
110
    public function s3ImageResize($filepath, $resize)
111
    {
112
        $imagepathinfo = $this->getPathinfo($filepath, $resize);
0 ignored issues
show
Bug introduced by
It seems like getPathinfo() 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...
113
        $S3 = new S3();
114
        // Exception = 存在していない場合
115
        $tmpFileName = Security::hash(rand() . Time::now()->i18nFormat('YYYY/MM/dd HH:ii:ss'));
116
        $tmpPath = Configure::read('ContentsFile.Setting.S3.workingDir') . $tmpFileName;
117
        // ベースのファイルを取得
118
        $baseObject = $S3->download($filepath);
119
        $fp = fopen($tmpPath, 'w');
120
        fwrite($fp, $baseObject['Body']);
121
        fclose($fp);
122
        if (!$this->imageResize($tmpPath, $resize)) {
0 ignored issues
show
Bug introduced by
The method imageResize() does not exist on ContentsFile\Model\Behav...ntentsFileBehaviorTrait. Did you maybe mean s3ImageResize()?

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...
123
            //失敗時はそのままのパスを返す(画像以外の可能性あり)
124
            unlink($tmpPath);
125
            return $filepath;
126
        }
127
        $resizeFileDir = Configure::read('ContentsFile.Setting.S3.workingDir') . 'contents_file_resize_' . $tmpFileName;
128
        $resizeFolder = new Folder($resizeFileDir);
129
        // 一つのはず
130
        $resizeImg = $resizeFolder->findRecursive()[0];
131
132
        // リサイズ画像をアップロード
133
        $S3->upload($resizeImg, $imagepathinfo['resize_filepath']);
134
135
        // tmpディレクトリの不要なディレクトリ/ファイルを削除
136
        $resizeFolder->delete();
137
        unlink($tmpPath);
138
        return $imagepathinfo['resize_filepath'];
139
    }
140
141
}
142