Completed
Push — master ( 142aea...16db13 )
by satoru
04:09 queued 02:02
created

ContentsFileHelper::makeStaticS3Url()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 19

Duplication

Lines 14
Ratio 53.85 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 26
rs 8.5806
cc 4
eloc 19
nc 5
nop 1
1
<?php
2
3
namespace ContentsFile\View\Helper;
4
5
use Cake\Core\Configure;
6
use Cake\View\Helper;
7
8
class ContentsFileHelper extends Helper {
9
10
    public $helpers = ['Html', 'Url'];
11
    private $defaultOption = [
12
        'target' => '_blank',
13
        'escape' => false
14
    ];
15
16
    /**
17
     * link
18
     * @author hagiwara
19
     * @param array $fileInfo
20
     * @param array $options
21
     * @param string $title
22
     */
23
    public function link($fileInfo, $options = [], $title = null)
24
    {
25
        if (empty($fileInfo)) {
26
            return '';
27
        }
28
        //一時パス用の設定
29
        if ($title === null) {
30
            $title = $fileInfo['file_name'];
31
        }
32
        if (isset($options['resize'])) {
33
            $fileInfo['resize'] = $options['resize'];
34
            unset($options['resize']);
35
        }
36
        $options = array_merge(
37
            $this->defaultOption,
38
            $options
39
        );
40
41
        return $this->Html->link(
0 ignored issues
show
Documentation introduced by
The property Html does not exist on object<ContentsFile\View...per\ContentsFileHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
42
            $title,
43
            $this->urlArray($fileInfo, $options),
44
            $options
45
        );
46
    }
47
48
    /**
49
     * image
50
     * @author hagiwara
51
     * @param array $fileInfo
52
     * @param array $options
53
     */
54
    public function image($fileInfo, $options = [])
55
    {
56
        if (empty($fileInfo)) {
57
            return '';
58
        }
59
        if (!empty($fileInfo)) {
60
            if (isset($options['resize'])) {
61
                $fileInfo['resize'] = $options['resize'];
62
                unset($options['resize']);
63
            }
64
            return $this->Html->image($this->urlArray($fileInfo, $options), $options);
0 ignored issues
show
Documentation introduced by
The property Html does not exist on object<ContentsFile\View...per\ContentsFileHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
        }
66
        return '';
67
    }
68
69
    /**
70
     * url
71
     * @author hagiwara
72
     * @param array $fileInfo
73
     * @param boolean $full
74
     * @param array $options
75
     */
76
    public function url($fileInfo, $full = false, $options = [])
77
    {
78
        if (empty($fileInfo)) {
79
            return [];
80
        }
81
        if (!isset($fileInfo['resize'])) {
82
            $fileInfo['resize'] = false;
83
        }
84
        return $this->Url->build($this->urlArray($fileInfo, $options), $full);
0 ignored issues
show
Documentation introduced by
The property Url does not exist on object<ContentsFile\View...per\ContentsFileHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
85
    }
86
87
    /**
88
     * urlArray
89
     * @author hagiwara
90
     * @param array $fileInfo
91
     */
92
    private function urlArray($fileInfo, $options)
93
    {
94
        if (!empty($fileInfo['tmp_file_name'])) {
95
            return [
96
                'controller' => 'contents_file',
97
                'action' => 'loader',
98
                'plugin' => 'ContentsFile',
99
                'model' => $fileInfo['model'],
100
                'field_name' => $fileInfo['field_name'],
101
                'tmp_file_name' => $fileInfo['tmp_file_name'],
102
                // prefixは無視する
103
                'prefix' => false,
104
            ];
105
        } else {
106
            if (!isset($fileInfo['resize'])) {
107
                $fileInfo['resize'] = false;
108
            }
109
            // S3のホスティングの場合
110
            if (
111
                array_key_exists('static_s3', $options) &&
112
                $options['static_s3'] == true &&
113
                Configure::read('ContentsFile.Setting.type') == 's3' &&
114
                !is_null(Configure::read('ContentsFile.Setting.S3.static_domain'))
115
            ) {
116
                return $this->makeStaticS3Url($fileInfo);
117
            } else {
118
                // loaderを通す場合
119
                return [
120
                    'controller' => 'contents_file',
121
                    'action' => 'loader',
122
                    'plugin' => 'ContentsFile',
123
                    'model' => $fileInfo['model'],
124
                    'field_name' => $fileInfo['field_name'],
125
                    'model_id' => $fileInfo['model_id'],
126
                    'resize' => $fileInfo['resize'],
127
                    // prefixは無視する
128
                    'prefix' => false,
129
                ];
130
            }
131
        }
132
    }
133
134
    /**
135
     * makeStaticS3Url
136
     * 静的ホスティング用のURL作成
137
     * @author hagiwara
138
     * @param array $fileInfo
139
     */
140
    private function makeStaticS3Url($fileInfo)
141
    {
142
        $staticS3Url = Configure::read('ContentsFile.Setting.S3.static_domain') . '/' . Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $fileInfo['model'] . '/' . $fileInfo['model_id'] . '/';
143
        if ($fileInfo['resize'] == false) {
144
            $staticS3Url .= $fileInfo['field_name'];
145
        } else {
146
            $resizeText = '';
147 View Code Duplication
            if (
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...
148
                empty($fileInfo['resize']['width'])
149
            ) {
150
                $resizeText .= '0';
151
            } else {
152
                $resizeText .= $fileInfo['resize']['width'];
153
            }
154
            $resizeText .= '_';
155 View Code Duplication
            if (
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...
156
                empty($fileInfo['resize']['height'])
157
            ) {
158
                $resizeText .= '0';
159
            } else {
160
                $resizeText .= $fileInfo['resize']['height'];
161
            }
162
            $staticS3Url .= 'contents_file_resize_' . $fileInfo['field_name'] . '/' . $resizeText;
163
        }
164
        return $staticS3Url;
165
    }
166
}
167