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

NormalContentsFileBehaviorTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 94
Duplicated Lines 7.45 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 3
dl 7
loc 94
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalParamCheck() 0 12 4
B normalFileSave() 7 25 6
B normalFileDelete() 0 18 5
A normalImageResize() 0 4 1

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
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
35
    /**
36
     * normalFileSave
37
     * ファイルを保存
38
     * @author hagiwara
39
     * @param array $fileInfo
40
     * @param array $fieldSettings
41
     * @param array $attachmentSaveData
42
     */
43
    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...
44
    {
45
        $newFiledir = Configure::read('ContentsFile.Setting.Normal.fileDir') . $attachmentSaveData['model'] . '/' . $attachmentSaveData['model_id'] . '/';
46
        $newFilepath = $newFiledir . $fileInfo['field_name'];
47
        if (
48
            !$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...
49
            !rename(Configure::read('ContentsFile.Setting.Normal.tmpDir') . $fileInfo['tmp_file_name'], $newFilepath)
50
        ) {
51
            return false;
52
        }
53
54
        //リサイズディレクトリはまず削除する
55
        $Folder = new Folder($newFilepath);
56
        $Folder->delete();
57
58
        //リサイズ画像作成
59 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...
60
            foreach ($fieldSettings['resize'] as $resizeSettings) {
61
                if (!$this->normalImageResize($newFilepath, $resizeSettings)) {
62
                    return false;
63
                }
64
            }
65
        }
66
        return true;
67
    }
68
69
    /**
70
     * normalFileDelete
71
     * 通常のファイル削除
72
     * @author hagiwara
73
     * @param string $modelName
74
     * @param integer $modelId
75
     * @param string $field
76
     */
77
    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...
78
    {
79
        // リサイズのディレクトリ
80
        $resizeDir = Configure::read('ContentsFile.Setting.Normal.fileDir') . $modelName . '/' . $modelId . '/' . 'contents_file_resize_' . $field . '/';
81
        if (is_dir($resizeDir)) {
82
            $deleteFolder = new Folder($resizeDir);
83
            if (!$deleteFolder->delete()) {
84
                return false;
85
            }
86
        }
87
88
        // 大元のファイル
89
        $deleteFile = Configure::read('ContentsFile.Setting.Normal.fileDir') . $modelName . '/' . $modelId . '/' . $field;
90
        if (file_exists($deleteFile) && !unlink($deleteFile)) {
91
            return false;
92
        }
93
        return true;
94
    }
95
96
    /**
97
     * normalImageResize
98
     * 通常のファイルのリサイズ
99
     * @author hagiwara
100
     * @param string $newFilepath
101
     * @param array $resizeSettings
102
     */
103
    private function normalImageResize($newFilepath, $resizeSettings)
104
    {
105
        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...
106
    }
107
}
108