Completed
Push — master ( 16db13...86107b )
by satoru
09:48 queued 07:28
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', 'Form'];
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
     * contentsFileHidden
89
     *
90
     * バリデーションに引っかかった際にファイルをそのまま送る用
91
     *
92
     * @author hagiwara
93
     * @param entity $entity
94
     * @param text $field
95
     */
96
    public function contentsFileHidden($entity, $field)
97
    {
98
        $contentFileField = 'contents_file_' . $field;
99
        $hiddenInput = '';
100
        if (!empty($entity->{$contentFileField})) {
101
            foreach ($entity->{$contentFileField} as $fieldParts => $v) {
102
                $hiddenInput .= $this->Form->input($contentFileField . '.' . $fieldParts, ['type' => 'hidden']);
0 ignored issues
show
Documentation introduced by
The property Form 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...
103
            }
104
        }
105
        return $hiddenInput;
106
    }
107
108
    /**
109
     * urlArray
110
     * @author hagiwara
111
     * @param array $fileInfo
112
     */
113
    private function urlArray($fileInfo, $options)
114
    {
115
        if (!empty($fileInfo['tmp_file_name'])) {
116
            return [
117
                'controller' => 'contents_file',
118
                'action' => 'loader',
119
                'plugin' => 'ContentsFile',
120
                'model' => $fileInfo['model'],
121
                'field_name' => $fileInfo['field_name'],
122
                'tmp_file_name' => $fileInfo['tmp_file_name'],
123
                // prefixは無視する
124
                'prefix' => false,
125
            ];
126
        } else {
127
            if (!isset($fileInfo['resize'])) {
128
                $fileInfo['resize'] = false;
129
            }
130
            // S3のホスティングの場合
131
            if (
132
                array_key_exists('static_s3', $options) &&
133
                $options['static_s3'] == true &&
134
                Configure::read('ContentsFile.Setting.type') == 's3' &&
135
                !is_null(Configure::read('ContentsFile.Setting.S3.static_domain'))
136
            ) {
137
                return $this->makeStaticS3Url($fileInfo);
138
            } else {
139
                // loaderを通す場合
140
                return [
141
                    'controller' => 'contents_file',
142
                    'action' => 'loader',
143
                    'plugin' => 'ContentsFile',
144
                    'model' => $fileInfo['model'],
145
                    'field_name' => $fileInfo['field_name'],
146
                    'model_id' => $fileInfo['model_id'],
147
                    'resize' => $fileInfo['resize'],
148
                    // prefixは無視する
149
                    'prefix' => false,
150
                ];
151
            }
152
        }
153
    }
154
155
    /**
156
     * makeStaticS3Url
157
     * 静的ホスティング用のURL作成
158
     * @author hagiwara
159
     * @param array $fileInfo
160
     */
161
    private function makeStaticS3Url($fileInfo)
162
    {
163
        $staticS3Url = Configure::read('ContentsFile.Setting.S3.static_domain') . '/' . Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $fileInfo['model'] . '/' . $fileInfo['model_id'] . '/';
164
        if ($fileInfo['resize'] == false) {
165
            $staticS3Url .= $fileInfo['field_name'];
166
        } else {
167
            $resizeText = '';
168 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...
169
                empty($fileInfo['resize']['width'])
170
            ) {
171
                $resizeText .= '0';
172
            } else {
173
                $resizeText .= $fileInfo['resize']['width'];
174
            }
175
            $resizeText .= '_';
176 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...
177
                empty($fileInfo['resize']['height'])
178
            ) {
179
                $resizeText .= '0';
180
            } else {
181
                $resizeText .= $fileInfo['resize']['height'];
182
            }
183
            $staticS3Url .= 'contents_file_resize_' . $fileInfo['field_name'] . '/' . $resizeText;
184
        }
185
        return $staticS3Url;
186
    }
187
}
188