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

S3ContentsFileControllerTrait::s3ResizeSet()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 13
nc 12
nop 2
1
<?php
2
namespace ContentsFile\Controller\Traits;
3
4
use Cake\Core\Configure;
5
use ContentsFile\Aws\S3;
6
7
/**
8
 * S3のファイルローダー周り
9
 * S3ContentsFileControllerTrait
10
 */
11
trait S3ContentsFileControllerTrait
12
{
13
    /**
14
     * s3Loader
15
     * S3用のファイルローダー
16
     * @author hagiwara
17
     */
18
    private function s3Loader($filename, $filepath)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
19
    {
20
        // S3より該当ファイルを取得
21
        $S3 = new S3();
22
        $fileObject = $S3->download($filepath);
23
        $topath = Configure::read('ContentsFile.Setting.Normal.tmpDir') . $filename;
24
        $fp = fopen($topath, 'w');
25
        fwrite($fp, $fileObject['Body']);
26
        fclose($fp);
27
28
        // ファイルの出力
29
        header('Content-Length: ' . filesize($topath));
30
        $fileContentType = $this->getMimeType($topath);
0 ignored issues
show
Bug introduced by
It seems like getMimeType() 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...
31
        header('Content-Type: ' . $fileContentType);
32
        @ob_end_clean(); // clean
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
        readfile($topath);
34
        // サーバー上にファイルを残しておく必要がないので削除する
35
        unlink($topath);
36
    }
37
38
    /**
39
     * s3TmpFilePath
40
     * S3のtmpのパス作成
41
     * @author hagiwara
42
     * @param string $filename
43
     */
44
    private function s3TmpFilePath($filename)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
45
    {
46
        return Configure::read('ContentsFile.Setting.S3.tmpDir') . '/' . $filename;
47
    }
48
49
    /**
50
     * s3FilePath
51
     * S3のファイルのパス作成
52
     * @author hagiwara
53
     * @param Entity $attachmentData
54
     */
55
    private function s3FilePath($attachmentData)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
56
    {
57
        return Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $attachmentData->model . '/' . $attachmentData->model_id . '/' . $attachmentData->field_name;
58
    }
59
60
61
    /**
62
     * s3ResizeSet
63
     * S3のリサイズ処理
64
     * @author hagiwara
65
     * @param string $filepath
66
     * @param array $resize
67
     */
68
    private function s3ResizeSet($filepath, $resize)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
69
    {
70
        if (empty($resize['width'])) {
71
            $resize['width'] = 0;
72
        }
73
        if (empty($resize['height'])) {
74
            $resize['height'] = 0;
75
        }
76
        //両方ゼロの場合はそのまま返す
77
        if ($resize['width'] == 0 && $resize['height'] == 0) {
78
            return $filepath;
79
        }
80
        $imagepathinfo = $this->baseModel->getPathinfo($filepath, $resize);
0 ignored issues
show
Bug introduced by
The property baseModel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
81
82
        $S3 = new S3();
83
        // 落としてこれる場合は存在している
84
        if ($S3->fileExists($imagepathinfo['resize_filepath'])) {
85
            return $imagepathinfo['resize_filepath'];
86
        } else {
87
            return $this->baseModel->s3ImageResize($imagepathinfo, $resize);
88
        }
89
    }
90
}
91