Completed
Branch s3_upload (82f26a)
by satoru
01:48
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
        //一時パス用の設定
26
        if ($title === null) {
27
            $title = $fileInfo['file_name'];
28
        }
29
        if (isset($options['resize'])) {
30
            $fileInfo['resize'] = $options['resize'];
31
            unset($options['resize']);
32
        }
33
        $options = array_merge(
34
            $this->defaultOption,
35
            $options
36
        );
37
38
        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...
39
            $title,
40
            $this->urlArray($fileInfo, $options),
41
            $options
42
        );
43
    }
44
45
    /**
46
     * image
47
     * @author hagiwara
48
     * @param array $fileInfo
49
     * @param array $options
50
     */
51
    public function image($fileInfo, $options = [])
52
    {
53
        if (!empty($fileInfo)) {
54
            if (isset($options['resize'])) {
55
                $fileInfo['resize'] = $options['resize'];
56
                unset($options['resize']);
57
            }
58
            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...
59
        }
60
        return '';
61
    }
62
63
    /**
64
     * url
65
     * @author hagiwara
66
     * @param array $fileInfo
67
     * @param boolean $full
68
     * @param array $options
69
     */
70
    public function url($fileInfo, $full = false, $options = [])
71
    {
72
        if (!isset($fileInfo['resize'])) {
73
            $fileInfo['resize'] = false;
74
        }
75
        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...
76
    }
77
78
    /**
79
     * urlArray
80
     * @author hagiwara
81
     * @param array $fileInfo
82
     */
83
    private function urlArray($fileInfo, $options)
84
    {
85
        if (!empty($fileInfo['tmp_file_name'])) {
86
            return [
87
                'controller' => 'contents_file',
88
                'action' => 'loader',
89
                'plugin' => 'ContentsFile',
90
                'model' => $fileInfo['model'],
91
                'field_name' => $fileInfo['field_name'],
92
                'tmp_file_name' => $fileInfo['tmp_file_name'],
93
            ];
94
        } else {
95
            if (!isset($fileInfo['resize'])) {
96
                $fileInfo['resize'] = false;
97
            }
98
            // S3のホスティングの場合
99
            if (
100
                array_key_exists('static_s3', $options) &&
101
                $options['static_s3'] == true &&
102
                Configure::read('ContentsFile.Setting.type') == 's3' &&
103
                !is_null(Configure::read('ContentsFile.Setting.S3.static_domain'))
104
            ) {
105
                return $this->makeStaticS3Url($fileInfo);
106
            } else {
107
                // loaderを通す場合
108
                return [
109
                    'controller' => 'contents_file',
110
                    'action' => 'loader',
111
                    'plugin' => 'ContentsFile',
112
                    'model' => $fileInfo['model'],
113
                    'field_name' => $fileInfo['field_name'],
114
                    'model_id' => $fileInfo['model_id'],
115
                    'resize' => $fileInfo['resize'],
116
                ];
117
            }
118
        }
119
    }
120
121
    /**
122
     * makeStaticS3Url
123
     * 静的ホスティング用のURL作成
124
     * @author hagiwara
125
     * @param array $fileInfo
126
     */
127
    private function makeStaticS3Url($fileInfo)
128
    {
129
        $staticS3Url = Configure::read('ContentsFile.Setting.S3.static_domain') . '/' . Configure::read('ContentsFile.Setting.S3.fileDir') . '/' . $fileInfo['model'] . '/' . $fileInfo['model_id'] . '/';
130
        if ($fileInfo['resize'] == false) {
131
            $staticS3Url .= $fileInfo['field_name'];
132
        } else {
133
            $resizeText = '';
134 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...
135
                empty($fileInfo['resize']['width'])
136
            ) {
137
                $resizeText .= '0';
138
            } else {
139
                $resizeText .= $fileInfo['resize']['width'];
140
            }
141
            $resizeText .= '_';
142 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...
143
                empty($fileInfo['resize']['height'])
144
            ) {
145
                $resizeText .= '0';
146
            } else {
147
                $resizeText .= $fileInfo['resize']['height'];
148
            }
149
            $staticS3Url .= 'contents_file_resize_' . $fileInfo['field_name'] . '/' . $resizeText;
150
        }
151
        return $staticS3Url;
152
    }
153
}
154