S3ContentsFileControllerTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 12.37 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 2
dl 12
loc 97
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A s3Loader() 0 22 1
A s3TmpFilePath() 0 4 1
A s3FilePath() 12 12 4
B s3ResizeSet() 0 22 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace ContentsFile\Controller\Traits;
3
4
use Cake\Core\Configure;
5
use Cake\ORM\Entity;
6
use ContentsFile\Aws\S3;
7
8
/**
9
 * S3のファイルローダー周り
10
 * S3ContentsFileControllerTrait
11
 */
12
trait S3ContentsFileControllerTrait
13
{
14
    /**
15
     * s3Loader
16
     * S3用のファイルローダー
17
     * @author hagiwara
18
     * @param string $filename
19
     * @param string $filepath
20
     * @return void
21
     */
22
    private function s3Loader(string $filename, string $filepath): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
23
    {
24
        // S3より該当ファイルを取得
25
        $S3 = new S3();
26
        $fileObject = $S3->download($filepath);
27
        $topath = Configure::read('ContentsFile.Setting.S3.workingDir') . $filename;
28
        $fp = fopen($topath, 'w');
29
        fwrite($fp, $fileObject['Body']);
30
        fclose($fp);
31
32
        // ファイルの出力
33
        $this->response = $this->response->withHeader('Content-Length', filesize($topath));
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...
34
        $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...
35
        $this->response = $this->response->withType($fileContentType);
36
        @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...
37
        $fp = fopen($topath, 'r');
38
        $body = fread($fp, filesize($topath));
39
        fclose($fp);
40
        $this->response->getBody()->write($body);
41
        // サーバー上にファイルを残しておく必要がないので削除する
42
        unlink($topath);
43
    }
44
45
    /**
46
     * s3TmpFilePath
47
     * S3のtmpのパス作成
48
     * @author hagiwara
49
     * @param string $filename
50
     * @return string
51
     */
52
    private function s3TmpFilePath($filename): string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
53
    {
54
        return Configure::read('ContentsFile.Setting.S3.tmpDir') . $filename;
55
    }
56
57
    /**
58
     * s3FilePath
59
     * S3のファイルのパス作成
60
     * @author hagiwara
61
     * @param Entity $attachmentData
62
     * @return string
63
     */
64 View Code Duplication
    private function s3FilePath(Entity $attachmentData): string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
65
    {
66
        $ext = '';
67
        if (Configure::read('ContentsFile.Setting.ext') === true) {
68
            $ext = '.' . (new \SplFileInfo($attachmentData->file_name))->getExtension();
69
        }
70
        if (Configure::read('ContentsFile.Setting.randomFile') === true && $attachmentData->file_random_path != '') {
71
            return Configure::read('ContentsFile.Setting.S3.fileDir') . $attachmentData->model . '/' . $attachmentData->model_id . '/' . $attachmentData->file_random_path . $ext;
72
        } else {
73
            return Configure::read('ContentsFile.Setting.S3.fileDir') . $attachmentData->model . '/' . $attachmentData->model_id . '/' . $attachmentData->field_name . $ext;
74
        }
75
    }
76
77
78
    /**
79
     * s3ResizeSet
80
     * S3のリサイズ処理
81
     * @author hagiwara
82
     * @param string $filepath
83
     * @param array $resize
84
     * @param string
85
     */
86
    private function s3ResizeSet(string $filepath, array $resize): string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
87
    {
88
        if (empty($resize['width'])) {
89
            $resize['width'] = 0;
90
        }
91
        if (empty($resize['height'])) {
92
            $resize['height'] = 0;
93
        }
94
        //両方ゼロの場合はそのまま返す
95
        if ($resize['width'] == 0 && $resize['height'] == 0) {
96
            return $filepath;
97
        }
98
        $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...
99
100
        $S3 = new S3();
101
        // 落としてこれる場合は存在している
102
        if ($S3->fileExists($imagepathinfo['resize_filepath'])) {
103
            return $imagepathinfo['resize_filepath'];
104
        } else {
105
            return $this->baseModel->s3ImageResize($filepath, $resize);
106
        }
107
    }
108
}
109