Completed
Pull Request — master (#25)
by satoru
02:14
created

normalTmpFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace ContentsFile\Controller\Traits;
4
5
use Cake\Core\Configure;
6
7
/**
8
 * 通常のファイルローダー周り
9
 * NormalContentsFileControllerTrait
10
 */
11
trait NormalContentsFileControllerTrait
12
{
13
    /**
14
     * normalLoader
15
     * 通常のローダー
16
     * @author hagiwara
17
     */
18
    private function normalLoader($filename, $filepath)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
19
    {
20
        $fileExt = null;
21
        if (preg_match('/\.([^\.]*)$/', $filename, $ext)) {
22
            if ($ext[1]) {
23
                $fileExt = strtolower($ext[1]);
24
            }
25
        }
26
27
        $this->response->header('Content-Length', filesize($filepath));
0 ignored issues
show
Bug introduced by
The property response 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...
28
        if (!empty($fileExt)) {
29
            $fileContentType = $this->getFileType($fileExt);
0 ignored issues
show
Bug introduced by
It seems like getFileType() 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...
30
        } else {
31
            $fileContentType = $this->getMimeType($filepath);
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...
32
        }
33
        $this->response->type($fileContentType);
34
        @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...
35
        $fp = fopen($filepath, 'r');
36
        $body = fread($fp, filesize($filepath));
37
        fclose($fp);
38
        $this->response->body($body);
39
    }
40
41
    /**
42
     * normalTmpFilePath
43
     * 通常用のtmpのパス作成
44
     * @author hagiwara
45
     * @param string $filename
46
     */
47
    private function normalTmpFilePath($filename)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
48
    {
49
        return Configure::read('ContentsFile.Setting.Normal.tmpDir') . $filename;
50
    }
51
52
    /**
53
     * normalFilePath
54
     * 通常用のファイルのパス作成
55
     * @author hagiwara
56
     * @param Entity $attachmentData
57
     */
58
    private function normalFilePath($attachmentData)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
59
    {
60
        return Configure::read('ContentsFile.Setting.Normal.fileDir') . $attachmentData->model . '/' . $attachmentData->model_id . '/' . $attachmentData->field_name;
61
    }
62
63
    /**
64
     * normalResizeSet
65
     * 通常のリサイズ処理
66
     * @author hagiwara
67
     * @param string $filepath
68
     * @param array $resize
69
     */
70
    private function normalResizeSet($filepath, $resize)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
71
    {
72
        if (empty($resize['width'])) {
73
            $resize['width'] = 0;
74
        }
75
        if (empty($resize['height'])) {
76
            $resize['height'] = 0;
77
        }
78
        //両方ゼロの場合はそのまま返す
79
        if ($resize['width'] == 0 && $resize['height'] == 0) {
80
            return $filepath;
81
        }
82
        $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...
83
84
        //ファイルの存在チェック
85
        if (file_exists($imagepathinfo['resize_filepath'])) {
86
            return $imagepathinfo['resize_filepath'];
87
        }
88
89
        //ない場合はリサイズを実行
90
        if (!$this->baseModel->imageResize($filepath, $resize)) {
91
            //失敗時はそのままのパスを返す(画像以外の可能性あり)
92
            return $filepath;
93
        }
94
        return $imagepathinfo['resize_filepath'];
95
    }
96
}
97