Completed
Pull Request — master (#13)
by satoru
02:12
created

ContentsFileController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
c 2
b 0
f 0
lcom 1
cbo 8
dl 0
loc 148
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B initialize() 0 19 6
B loader() 0 42 6
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
        //Entityに接続して設定値を取得
51
        $this->baseModel = TableRegistry::get($this->request->query['model']);
52
53
        // このレベルで切り出す
54
        $fieldName = $this->request->query['field_name'];
55
        if (!empty($this->request->query['tmp_file_name'])) {
56
            $filename = $this->request->query['tmp_file_name'];
57
            $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'TmpFilePath'}($filename);
58
            Configure::read('ContentsFile.Setting.Normal.tmpDir') . $filename;
59
        } elseif (!empty($this->request->query['model_id'])) {
60
            //表示条件をチェックする
61
            $checkMethodName = 'contentsFileCheck' . Inflector::camelize($fieldName);
62
            if (method_exists($this->baseModel, $checkMethodName)) {
63
                //エラーなどの処理はTableに任せる
64
                $this->baseModel->{$checkMethodName}($this->request->query['model_id']);
65
            }
66
            //attachementからデータを取得
67
            $attachmentModel = TableRegistry::get('Attachments');
68
            $attachmentData = $attachmentModel->find('all')
69
                ->where(['model' => $this->request->query['model']])
70
                ->where(['model_id' => $this->request->query['model_id']])
71
                ->where(['field_name' => $this->request->query['field_name']])
72
                ->first()
73
            ;
74
            if (empty($attachmentData)) {
75
                throw new NotFoundException('404 error');
76
            }
77
            $filename = $attachmentData->file_name;
78
            $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'FilePath'}($attachmentData);
79
80
            //通常のセットの時のみresize設定があれば見る
81
            if (!empty($this->request->query['resize'])) {
82
                $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'ResizeSet'}($filepath, $this->request->query['resize']);
83
            }
84
        }
85
86
        $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...
87
    }
88
89
    /**
90
     * getFileType
91
     * @author hagiwara
92
     * @param string $ext
93
     */
94
    private function getFileType($ext)
95
    {
96
        $aContentTypes = [
97
            'txt'=>'text/plain',
98
            'htm'=>'text/html',
99
            'html'=>'text/html',
100
            'jpg'=>'image/jpeg',
101
            'jpeg'=>'image/jpeg',
102
            'gif'=>'image/gif',
103
            'png'=>'image/png',
104
            'bmp'=>'image/x-bmp',
105
            'ai'=>'application/postscript',
106
            'psd'=>'image/x-photoshop',
107
            'eps'=>'application/postscript',
108
            'pdf'=>'application/pdf',
109
            'swf'=>'application/x-shockwave-flash',
110
            'lzh'=>'application/x-lha-compressed',
111
            'zip'=>'application/x-zip-compressed',
112
            'sit'=>'application/x-stuffit'
113
        ];
114
        $sContentType = 'application/octet-stream';
115
116
        if (!empty($aContentTypes[$ext])) {
117
            $sContentType = $aContentTypes[$ext];
118
        }
119
        return $sContentType;
120
    }
121
122
    /**
123
     * getMimeType
124
     * @author hagiwara
125
     * @param string $filename
126
     */
127
    private function getMimeType($filename)
128
    {
129
        $aContentTypes = [
130
            'txt'=>'text/plain',
131
            'htm'=>'text/html',
132
            'html'=>'text/html',
133
            'jpg'=>'image/jpeg',
134
            'jpeg'=>'image/jpeg',
135
            'gif'=>'image/gif',
136
            'png'=>'image/png',
137
            'bmp'=>'image/x-bmp',
138
            'ai'=>'application/postscript',
139
            'psd'=>'image/x-photoshop',
140
            'eps'=>'application/postscript',
141
            'pdf'=>'application/pdf',
142
            'swf'=>'application/x-shockwave-flash',
143
            'lzh'=>'application/x-lha-compressed',
144
            'zip'=>'application/x-zip-compressed',
145
            'sit'=>'application/x-stuffit'
146
        ];
147
        $sContentType = 'application/octet-stream';
148
149
        if (($pos = strrpos($filename, ".")) !== false) {
150
            // 拡張子がある場合
151
            $ext = strtolower(substr($filename, $pos + 1));
152
            if (strlen($ext)) {
153
                return $aContentTypes[$ext] ? $aContentTypes[$ext] : $sContentType;
154
            }
155
        }
156
        return $sContentType;
157
    }
158
159
}
160