ContentsFileHelper::urlArray()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 8.2012
c 0
b 0
f 0
cc 7
nc 5
nop 2
1
<?php
2
3
namespace ContentsFile\View\Helper;
4
5
use Cake\Core\Configure;
6
use Cake\ORM\Entity;
7
use Cake\View\Helper;
8
9
class ContentsFileHelper extends Helper {
10
11
    public $helpers = ['Html', 'Url', 'Form'];
12
    private $defaultOption = [
13
        'target' => '_blank',
14
        'escape' => false,
15
        'download' => false
16
    ];
17
18
    /**
19
     * link
20
     * @author hagiwara
21
     * @param array|null $fileInfo
22
     * @param array $options
23
     * @param string $title
24
     * @return string|null
25
     */
26
    public function link($fileInfo, array $options = [], string $title = null): string
27
    {
28
        if (empty($fileInfo)) {
29
            return '';
30
        }
31
        //一時パス用の設定
32
        if ($title === null) {
33
            $title = $fileInfo['file_name'];
34
        }
35 View Code Duplication
        if (isset($options['resize'])) {
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...
36
            $fileInfo['resize'] = $options['resize'];
37
            unset($options['resize']);
38
        }
39
        $options = array_merge(
40
            $this->defaultOption,
41
            $options
42
        );
43
44
        $urlOption = $options;
45
        $linkOptions = $options;
46
        // download属性がいるとEdgeでダウンロード属性が優先されてしまう
47
        unset($linkOptions['download']);
48
49
        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...
50
            $title,
51
            $this->urlArray($fileInfo, $urlOption),
52
            $linkOptions
53
        );
54
    }
55
56
    /**
57
     * image
58
     * @author hagiwara
59
     * @param array|null $fileInfo
60
     * @param array $options
61
     * @return string
62
     */
63
    public function image($fileInfo, array $options = []): string
64
    {
65
        if (empty($fileInfo)) {
66
            return '';
67
        }
68
        $options = array_merge(
69
            $this->defaultOption,
70
            $options
71
        );
72
        if (!empty($fileInfo)) {
73 View Code Duplication
            if (isset($options['resize'])) {
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...
74
                $fileInfo['resize'] = $options['resize'];
75
                unset($options['resize']);
76
            }
77
            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...
78
        }
79
        return '';
80
    }
81
82
    /**
83
     * url
84
     * @author hagiwara
85
     * @param array|null $fileInfo
86
     * @param boolean $full
87
     * @param array $options
88
     * @return string
89
     */
90
    public function url($fileInfo, bool $full = false, array $options = []): string
91
    {
92
        if (empty($fileInfo)) {
93
            return [];
94
        }
95 View Code Duplication
        if (isset($options['resize'])) {
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...
96
            $fileInfo['resize'] = $options['resize'];
97
            unset($options['resize']);
98
        }
99
        $options = array_merge(
100
            $this->defaultOption,
101
            $options
102
        );
103
        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...
104
    }
105
106
    /**
107
     * contentsFileHidden
108
     *
109
     * バリデーションに引っかかった際にファイルをそのまま送る用
110
     *
111
     * @author hagiwara
112
     * @param array|null $contentFileData
113
     * @param string $field
114
     * @return string
115
     */
116
    public function contentsFileHidden($contentFileData, string $field): string
117
    {
118
        $hiddenInput = '';
119
        if (!empty($contentFileData)) {
120
            foreach ($contentFileData as $fieldParts => $v) {
121
                $hiddenInput .= $this->Form->input($field . '.' . $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...
122
            }
123
        }
124
        return $hiddenInput;
125
    }
126
127
    /**
128
     * urlArray
129
     * @author hagiwara
130
     * @param array|null $fileInfo
131
     * @param array $options
132
     * @return array
133
     */
134
    private function urlArray($fileInfo, array $options): array
135
    {
136
        if (!empty($fileInfo['tmp_file_name'])) {
137
            return [
138
                'controller' => 'contents_file',
139
                'action' => 'loader',
140
                'plugin' => 'ContentsFile',
141
                // prefixは無視する
142
                'prefix' => false,
143
                '?' => [
144
                    'model' => $fileInfo['model'],
145
                    'field_name' => $fileInfo['field_name'],
146
                    'tmp_file_name' => $fileInfo['tmp_file_name'],
147
                    'download' => $options['download'],
148
                ]
149
            ];
150
        } else {
151
            if (!isset($fileInfo['resize'])) {
152
                $fileInfo['resize'] = false;
153
            }
154
            // S3のホスティングの場合
155
            // downloadとかはつけられないので無視
156
            if (
157
                array_key_exists('static_s3', $options) &&
158
                $options['static_s3'] == true &&
159
                Configure::read('ContentsFile.Setting.type') == 's3' &&
160
                !is_null(Configure::read('ContentsFile.Setting.S3.static_domain'))
161
            ) {
162
                return $this->makeStaticS3Url($fileInfo);
163
            } else {
164
                // loaderを通す場合
165
                return [
166
                    'controller' => 'contents_file',
167
                    'action' => 'loader',
168
                    'plugin' => 'ContentsFile',
169
                    // prefixは無視する
170
                    'prefix' => false,
171
                    '?' => [
172
                        'model' => $fileInfo['model'],
173
                        'field_name' => $fileInfo['field_name'],
174
                        'model_id' => $fileInfo['model_id'],
175
                        'resize' => $fileInfo['resize'],
176
                        'download' => $options['download'],
177
                    ]
178
                ];
179
            }
180
        }
181
    }
182
183
    /**
184
     * makeStaticS3Url
185
     * 静的ホスティング用のURL作成
186
     * @author hagiwara
187
     * @param array $fileInfo
188
     * @return string
189
     */
190
    private function makeStaticS3Url(array $fileInfo): string
191
    {
192
        $staticS3Url = Configure::read('ContentsFile.Setting.S3.static_domain') . '/' . Configure::read('ContentsFile.Setting.S3.fileDir') . $fileInfo['model'] . '/' . $fileInfo['model_id'] . '/';
193
        if ($fileInfo['resize'] == false) {
194
            if (Configure::read('ContentsFile.Setting.randomFile') === true && $fileInfo['file_random_path'] != '') {
195
                $staticS3Url .= $fileInfo['file_random_path'];
196
            } else {
197
                $staticS3Url .= $fileInfo['field_name'];
198
            }
199
        } else {
200
            $resizeText = '';
201 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...
202
                empty($fileInfo['resize']['width'])
203
            ) {
204
                $resizeText .= '0';
205
            } else {
206
                $resizeText .= $fileInfo['resize']['width'];
207
            }
208
            $resizeText .= '_';
209 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...
210
                empty($fileInfo['resize']['height'])
211
            ) {
212
                $resizeText .= '0';
213
            } else {
214
                $resizeText .= $fileInfo['resize']['height'];
215
            }
216
            if (Configure::read('ContentsFile.Setting.randomFile') === true && $fileInfo['file_random_path'] != '') {
217
                $staticS3Url .= 'contents_file_resize_' . $fileInfo['file_random_path'] . '/' . $resizeText;
218
            } else {
219
                $staticS3Url .= 'contents_file_resize_' . $fileInfo['field_name'] . '/' . $resizeText;
220
            }
221
        }
222
        return $staticS3Url;
223
    }
224
225
    /* ここからはDD専用のHelper */
226
    /**
227
     * displayFilename
228
     * 静的ホスティング用のURL作成
229
     * @author hagiwara
230
     * @param Entity $entity
231
     * @param array $field
232
     * @return string
233
     */
234
    public function displayFilename(Entity $entity, string $field): string
235
    {
236
        if (!is_null($entity->{'contents_file_' . $field . '_filename'})) {
237
            return $entity->{'contents_file_' . $field . '_filename'};
238
        } elseif (is_array($entity->{'contents_file_' . $field})) {
239
            return $entity->{'contents_file_' . $field}['file_name'];
240
        } else {
241
            return '';
242
        }
243
    }
244
}
245