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

ContentsFileController::__resizeSet()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
rs 6.7272
cc 7
eloc 13
nc 16
nop 2
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
     * loader
20
     * @author hagiwara
21
     */
22
    public function loader()
23
    {
24
        $this->autoRender = false;
25
26
        //Entityに接続して設定値を取得
27
        $this->baseModel = TableRegistry::get($this->request->query['model']);
28
29
        // このレベルで切り出す
30
        $fieldName = $this->request->query['field_name'];
31
        if (!empty($this->request->query['tmp_file_name'])) {
32
            $filename = $this->request->query['tmp_file_name'];
33
            $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'TmpFilePath'}($filename);
34
            Configure::read('ContentsFile.Setting.Normal.tmpDir') . $filename;
35
        } elseif (!empty($this->request->query['model_id'])) {
36
            //表示条件をチェックする
37
            $checkMethodName = 'contentsFileCheck' . Inflector::camelize($fieldName);
38
            if (method_exists($this->baseModel, $checkMethodName)) {
39
                //エラーなどの処理はTableに任せる
40
                $this->baseModel->{$checkMethodName}($this->request->query['model_id']);
41
            }
42
            //attachementからデータを取得
43
            $attachmentModel = TableRegistry::get('Attachments');
44
            $attachmentData = $attachmentModel->find('all')
45
                ->where(['model' => $this->request->query['model']])
46
                ->where(['model_id' => $this->request->query['model_id']])
47
                ->where(['field_name' => $this->request->query['field_name']])
48
                ->first()
49
            ;
50
            if (empty($attachmentData)) {
51
                throw new NotFoundException('404 error');
52
            }
53
            $filename = $attachmentData->file_name;
54
            $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'FilePath'}($attachmentData);
55
56
            //通常のセットの時のみresize設定があれば見る
57
            if (!empty($this->request->query['resize'])) {
58
                $filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'ResizeSet'}($filepath, $this->request->query['resize']);
59
            }
60
        }
61
62
        $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...
63
    }
64
65
    /**
66
     * getFileType
67
     * @author hagiwara
68
     * @param string $ext
69
     */
70
    private function getFileType($ext)
71
    {
72
        $aContentTypes = [
73
            'txt'=>'text/plain',
74
            'htm'=>'text/html',
75
            'html'=>'text/html',
76
            'jpg'=>'image/jpeg',
77
            'jpeg'=>'image/jpeg',
78
            'gif'=>'image/gif',
79
            'png'=>'image/png',
80
            'bmp'=>'image/x-bmp',
81
            'ai'=>'application/postscript',
82
            'psd'=>'image/x-photoshop',
83
            'eps'=>'application/postscript',
84
            'pdf'=>'application/pdf',
85
            'swf'=>'application/x-shockwave-flash',
86
            'lzh'=>'application/x-lha-compressed',
87
            'zip'=>'application/x-zip-compressed',
88
            'sit'=>'application/x-stuffit'
89
        ];
90
        $sContentType = 'application/octet-stream';
91
92
        if (!empty($aContentTypes[$ext])) {
93
            $sContentType = $aContentTypes[$ext];
94
        }
95
        return $sContentType;
96
    }
97
98
    /**
99
     * getMimeType
100
     * @author hagiwara
101
     * @param string $filename
102
     */
103
    private function getMimeType($filename)
104
    {
105
        $aContentTypes = [
106
            'txt'=>'text/plain',
107
            'htm'=>'text/html',
108
            'html'=>'text/html',
109
            'jpg'=>'image/jpeg',
110
            'jpeg'=>'image/jpeg',
111
            'gif'=>'image/gif',
112
            'png'=>'image/png',
113
            'bmp'=>'image/x-bmp',
114
            'ai'=>'application/postscript',
115
            'psd'=>'image/x-photoshop',
116
            'eps'=>'application/postscript',
117
            'pdf'=>'application/pdf',
118
            'swf'=>'application/x-shockwave-flash',
119
            'lzh'=>'application/x-lha-compressed',
120
            'zip'=>'application/x-zip-compressed',
121
            'sit'=>'application/x-stuffit'
122
        ];
123
        $sContentType = 'application/octet-stream';
124
125
        if (($pos = strrpos($filename, ".")) !== false) {
126
            // 拡張子がある場合
127
            $ext = strtolower(substr($filename, $pos + 1));
128
            if (strlen($ext)) {
129
                return $aContentTypes[$ext] ? $aContentTypes[$ext] : $sContentType;
130
            }
131
        }
132
        return $sContentType;
133
    }
134
135
}
136