Completed
Pull Request — master (#14)
by satoru
01:59
created

ContentsFileController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 20
c 3
b 0
f 0
lcom 1
cbo 8
dl 0
loc 156
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B initialize() 0 19 6
C loader() 0 50 8
B getFileType() 0 27 2
B getMimeType() 0 31 4
1
<?php
2
3
namespace ContentsFile\Controller;
4
5
use Cake\Core\Configure;
6
use Cake\ORM\TableRegistry;
7
use Cake\Utility\Inflector;
8
use ContentsFile\Controller\AppController;
9
use ContentsFile\Controller\Traits\NormalContentsFileControllerTrait;
10
use ContentsFile\Controller\Traits\S3ContentsFileControllerTrait;
11
12
class ContentsFileController extends AppController
13
{
14
    use S3ContentsFileControllerTrait;
15
    use NormalContentsFileControllerTrait;
16
    private $baseModel;
17
18
    /**
19
     * initialize
20
     * Configureの最後のスラッシュの設定
21
     */
22
    public function initialize()
23
    {
24
        // /が最後についていない場合はつける
25
        if (!preg_match('#/$#', Configure::read('ContentsFile.Setting.Normal.tmpDir'))) {
26
            Configure::write('ContentsFile.Setting.Normal.tmpDir', Configure::read('ContentsFile.Setting.Normal.tmpDir') . '/');
27
        }
28
        if (!preg_match('#/$#', Configure::read('ContentsFile.Setting.Normal.fileDir'))) {
29
            Configure::write('ContentsFile.Setting.Normal.fileDir', Configure::read('ContentsFile.Setting.Normal.fileDir') . '/');
30
        }
31
        if (!preg_match('#/$#', Configure::read('ContentsFile.Setting.S3.tmpDir'))) {
32
            Configure::write('ContentsFile.Setting.S3.tmpDir', Configure::read('ContentsFile.Setting.S3.tmpDir') . '/');
33
        }
34
        if (!preg_match('#/$#', Configure::read('ContentsFile.Setting.S3.fileDir'))) {
35
            Configure::write('ContentsFile.Setting.S3.fileDir', Configure::read('ContentsFile.Setting.S3.fileDir') . '/');
36
        }
37
        if (!preg_match('#/$#', Configure::read('ContentsFile.Setting.S3.workingDir'))) {
38
            Configure::write('ContentsFile.Setting.S3.workingDir', Configure::read('ContentsFile.Setting.S3.workingDir'). '/');
39
        }
40
    }
41
42
    /**
43
     * loader
44
     * @author hagiwara
45
     */
46
    public function loader()
47
    {
48
        $this->autoRender = false;
49
50
        // 必要なパラメータがない場合はエラー
51
        if (
52
            empty($this->request->query['model']) ||
53
            empty($this->request->query['field_name'])
54
        ) {
55
            throw new NotFoundException('404 error');
56
        }
57
58
        //Entityに接続して設定値を取得
59
        $this->baseModel = TableRegistry::get($this->request->query['model']);
60
61
        // このレベルで切り出す
62
        $fieldName = $this->request->query['field_name'];
63
        if (!empty($this->request->query['tmp_file_name'])) {
64
            $filename = $this->request->query['tmp_file_name'];
65
            $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'TmpFilePath'}($filename);
66
            Configure::read('ContentsFile.Setting.Normal.tmpDir') . $filename;
67
        } elseif (!empty($this->request->query['model_id'])) {
68
            //表示条件をチェックする
69
            $checkMethodName = 'contentsFileCheck' . Inflector::camelize($fieldName);
70
            if (method_exists($this->baseModel, $checkMethodName)) {
71
                //エラーなどの処理はTableに任せる
72
                $this->baseModel->{$checkMethodName}($this->request->query['model_id']);
73
            }
74
            //attachementからデータを取得
75
            $attachmentModel = TableRegistry::get('Attachments');
76
            $attachmentData = $attachmentModel->find('all')
77
                ->where(['model' => $this->request->query['model']])
78
                ->where(['model_id' => $this->request->query['model_id']])
79
                ->where(['field_name' => $this->request->query['field_name']])
80
                ->first()
81
            ;
82
            if (empty($attachmentData)) {
83
                throw new NotFoundException('404 error');
84
            }
85
            $filename = $attachmentData->file_name;
86
            $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'FilePath'}($attachmentData);
87
88
            //通常のセットの時のみresize設定があれば見る
89
            if (!empty($this->request->query['resize'])) {
90
                $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'ResizeSet'}($filepath, $this->request->query['resize']);
91
            }
92
        }
93
94
        $this->{Configure::read('ContentsFile.Setting.type') . 'Loader'}($filename, $filepath);
0 ignored issues
show
Bug introduced by
The variable $filename does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $filepath does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
95
    }
96
97
    /**
98
     * getFileType
99
     * @author hagiwara
100
     * @param string $ext
101
     */
102
    private function getFileType($ext)
103
    {
104
        $aContentTypes = [
105
            'txt'=>'text/plain',
106
            'htm'=>'text/html',
107
            'html'=>'text/html',
108
            'jpg'=>'image/jpeg',
109
            'jpeg'=>'image/jpeg',
110
            'gif'=>'image/gif',
111
            'png'=>'image/png',
112
            'bmp'=>'image/x-bmp',
113
            'ai'=>'application/postscript',
114
            'psd'=>'image/x-photoshop',
115
            'eps'=>'application/postscript',
116
            'pdf'=>'application/pdf',
117
            'swf'=>'application/x-shockwave-flash',
118
            'lzh'=>'application/x-lha-compressed',
119
            'zip'=>'application/x-zip-compressed',
120
            'sit'=>'application/x-stuffit'
121
        ];
122
        $sContentType = 'application/octet-stream';
123
124
        if (!empty($aContentTypes[$ext])) {
125
            $sContentType = $aContentTypes[$ext];
126
        }
127
        return $sContentType;
128
    }
129
130
    /**
131
     * getMimeType
132
     * @author hagiwara
133
     * @param string $filename
134
     */
135
    private function getMimeType($filename)
136
    {
137
        $aContentTypes = [
138
            'txt'=>'text/plain',
139
            'htm'=>'text/html',
140
            'html'=>'text/html',
141
            'jpg'=>'image/jpeg',
142
            'jpeg'=>'image/jpeg',
143
            'gif'=>'image/gif',
144
            'png'=>'image/png',
145
            'bmp'=>'image/x-bmp',
146
            'ai'=>'application/postscript',
147
            'psd'=>'image/x-photoshop',
148
            'eps'=>'application/postscript',
149
            'pdf'=>'application/pdf',
150
            'swf'=>'application/x-shockwave-flash',
151
            'lzh'=>'application/x-lha-compressed',
152
            'zip'=>'application/x-zip-compressed',
153
            'sit'=>'application/x-stuffit'
154
        ];
155
        $sContentType = 'application/octet-stream';
156
157
        if (($pos = strrpos($filename, ".")) !== false) {
158
            // 拡張子がある場合
159
            $ext = strtolower(substr($filename, $pos + 1));
160
            if (strlen($ext)) {
161
                return $aContentTypes[$ext] ? $aContentTypes[$ext] : $sContentType;
162
            }
163
        }
164
        return $sContentType;
165
    }
166
167
}
168