Completed
Pull Request — master (#36)
by satoru
01:53
created

ContentsFileHelper::link()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 4
Ratio 16.67 %

Importance

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