Passed
Push — master ( 091b64...dd72cb )
by Nicolaas
09:12 queued 14s
created

PerfectCMSImageLink()   C

Complexity

Conditions 12
Paths 21

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 12
eloc 19
c 6
b 0
f 0
nc 21
nop 4
dl 0
loc 38
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sunnysideup\PerfectCmsImages\Model\File;
4
5
use SilverStripe\Assets\Folder;
6
use SilverStripe\Assets\Image;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Convert;
10
use SilverStripe\ORM\DataExtension;
11
use SilverStripe\ORM\FieldType\DBField;
12
use SilverStripe\ORM\FieldType\DBHTMLText;
13
use SilverStripe\View\ArrayData;
14
use Sunnysideup\PerfectCmsImages\Api\ImageManipulations;
15
use Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages;
0 ignored issues
show
Bug introduced by
The type Sunnysideup\PerfectCmsImages\Api\PerfectCMSImages was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
/**
18
 * defines the image sizes
19
 * and default upload folder.
20
 */
21
class PerfectCmsImageDataExtension extends DataExtension
22
{
23
    /**
24
     * background image for padded images...
25
     *
26
     * @var string
27
     */
28
    private static $perfect_cms_images_background_padding_color = '#cccccc';
29
30
    /*
31
     * details of the images
32
     *     - width: 3200
33
     *     - height: 3200
34
     *     - folder: "myfolder"
35
     *     - filetype: "try jpg"
36
     *     - enforce_size: false
37
     *     - folder: my-image-folder-a
38
     *     - filetype: "jpg or a png with a transparant background"
39
     *     - use_retina: true
40
     *     - padding_bg_colour: '#dddddd'
41
     *     - crop: true
42
     *     - move_to_right_folder: true
43
     *     - loading_style: 'eager'
44
     *     - used_by:
45
     *       - MyClass.MyHasOne
46
     *       - MyOtherClass.MyHasManyMethod
47
     *       - MyOtherClass.MyManyManyRel
48
     * @var array
49
     */
50
    private static $perfect_cms_images_image_definitions = [];
51
52
    private static $casting = [
53
        'PerfectCMSImageTag' => 'HTMLText',
54
    ];
55
56
    /**
57
     * @param string $name       PerfectCMSImages name
58
     * @param bool   $inline     for use within existing image tag - optional
59
     * @param string $alt        alt tag for image -optional
60
     * @param string $attributes additional attributes
61
     *
62
     * @return string (HTML)
63
     */
64
    public function getPerfectCMSImageTag(string $name, $inline = false, ?string $alt = '', ?string $attributes = '')
65
    {
66
        return $this->PerfectCMSImageTag($name, $inline, $alt, $attributes);
67
    }
68
69
    /**
70
     * @param string $name       PerfectCMSImages name
71
     * @param bool   $inline     for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also...
72
     * @param string $alt        alt tag for image -optional
73
     * @param string $attributes additional attributes
74
     *
75
     * @return string (HTML)
76
     */
77
    public function PerfectCMSImageTag(string $name, $inline = false, ?string $alt = '', ?string $attributes = '')
78
    {
79
        $arrayData = $this->getPerfectCMSImageTagArrayData($name, $inline, $alt, $attributes);
80
        $template = 'Includes/PerfectCMSImageTag';
81
        if (true === $inline || 1 === (int) $inline || 'true' === strtolower($inline)) {
0 ignored issues
show
Bug introduced by
$inline of type false is incompatible with the type string expected by parameter $string of strtolower(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        if (true === $inline || 1 === (int) $inline || 'true' === strtolower(/** @scrutinizer ignore-type */ $inline)) {
Loading history...
82
            $template .= 'Inline';
83
        }
84
85
        return DBField::create_field('HTMLText', $arrayData->renderWith($template));
86
    }
87
88
    /**
89
     * @param string $name       PerfectCMSImages name
90
     * @param bool   $inline     for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also...
91
     * @param string $alt        alt tag for image -optional
92
     * @param string $attributes additional attributes
93
     *
94
     * @return ArrayData
95
     */
96
    public function PerfectCMSImageTagArrayData(string $name, $inline = false, ?string $alt = '', ?string $attributes = '')
97
    {
98
        return $this->getPerfectCMSImageTagArrayData($name, $inline, $alt, $attributes);
99
    }
100
101
    /**
102
     * @param string $name       PerfectCMSImages name
103
     * @param bool   $inline     for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also...
104
     * @param string $alt        alt tag for image -optional
105
     * @param string $attributes additional attributes
106
     *
107
     * @return ArrayData
108
     */
109
    public function getPerfectCMSImageTagArrayData(string $name, $inline = false, ?string $alt = '', ?string $attributes = '')
0 ignored issues
show
Unused Code introduced by
The parameter $inline is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

109
    public function getPerfectCMSImageTagArrayData(string $name, /** @scrutinizer ignore-unused */ $inline = false, ?string $alt = '', ?string $attributes = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        $retinaLink = $this->PerfectCMSImageLinkRetina($name);
0 ignored issues
show
Unused Code introduced by
The assignment to $retinaLink is dead and can be removed.
Loading history...
112
        $nonRetinaLink = $this->PerfectCMSImageLinkNonRetina($name);
0 ignored issues
show
Unused Code introduced by
The assignment to $nonRetinaLink is dead and can be removed.
Loading history...
113
114
        $width = PerfectCMSImages::get_width($name, true);
115
        $height = PerfectCMSImages::get_height($name, true);
116
117
        $hasWebP = (bool) Config::inst()->get(ImageManipulations::class, 'webp_enabled');
118
        $hasMobile = PerfectCMSImages::has_mobile($name);
119
        $mobileRetinaLink = '';
120
        $mobileNonRetinaLink = '';
121
        $mobileMediaWidth = '';
122
123
        $retinaLink = $this->PerfectCMSImageLinkRetina($name);
124
        $nonRetinaLink = $this->PerfectCMSImageLinkNonRetina($name);
125
        if ($hasMobile) {
126
            $mobileRetinaLink = $this->PerfectCMSImageLinkRetinaForMobile($name);
127
            $mobileNonRetinaLink = $this->PerfectCMSImageLinkNonRetinaForMobile($name);
128
        }
129
        if ($hasWebP) {
130
            $retinaLinkWebP = $this->PerfectCMSImageLinkRetinaWebP($name);
131
            $nonRetinaLinkWebP = $this->PerfectCMSImageLinkNonRetinaWebP($name);
132
            if ($hasMobile) {
133
                $mobileRetinaLinkWebP = $this->PerfectCMSImageLinkRetinaWebPForMobile($name);
134
                $mobileNonRetinaLinkWebP = $this->PerfectCMSImageLinkNonRetinaWebPForMobile($name);
135
            }
136
        }
137
138
        if ($hasMobile) {
139
            $mobileMediaWidth = PerfectCMSImages::get_mobile_media_width($name);
140
        }
141
142
        if (! $alt) {
143
            $alt = $this->getOwner()->Title;
144
        }
145
146
        return ArrayData::create(
147
            [
148
                'MobileMediaWidth' => $mobileMediaWidth,
149
                'Width' => $width,
150
                'Height' => $height,
151
                'Alt' => Convert::raw2att($alt),
152
                'MobileRetinaLink' => $mobileRetinaLink,
153
                'MobileNonRetinaLink' => $mobileNonRetinaLink,
154
                'RetinaLink' => $retinaLink,
155
                'NonRetinaLink' => $nonRetinaLink,
156
                'MobileRetinaLinkWebP' => $mobileRetinaLinkWebP,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mobileRetinaLinkWebP does not seem to be defined for all execution paths leading up to this point.
Loading history...
157
                'MobileNonRetinaLinkWebP' => $mobileNonRetinaLinkWebP,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mobileNonRetinaLinkWebP does not seem to be defined for all execution paths leading up to this point.
Loading history...
158
                'RetinaLinkWebP' => $retinaLinkWebP,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $retinaLinkWebP does not seem to be defined for all execution paths leading up to this point.
Loading history...
159
                'NonRetinaLinkWebP' => $nonRetinaLinkWebP,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $nonRetinaLinkWebP does not seem to be defined for all execution paths leading up to this point.
Loading history...
160
                'Type' => $this->owner->getMimeType(),
161
                'HasWebP' => $hasWebP,
162
                'Attributes' => DBField::create_field('HTMLText', $attributes),
163
            ]
164
        );
165
    }
166
167
    /**
168
     * @param string $name of Image Field template
169
     *
170
     * @return string (link)
171
     */
172
    public function PerfectCMSImageLinkNonRetina(string $name): string
173
    {
174
        return $this->PerfectCMSImageLink($name);
175
    }
176
177
    /**
178
     * @param string $name of Image Field template
179
     *
180
     * @return string (link)
181
     */
182
    public function PerfectCMSImageLinkRetina(string $name): string
183
    {
184
        return $this->PerfectCMSImageLink($name, true);
185
    }
186
187
    /**
188
     * @param string $name of Image Field template
189
     *
190
     * @return string (link)
191
     */
192
    public function PerfectCMSImageLinkNonRetinaWebP(string $name): string
193
    {
194
        return $this->PerfectCMSImageLink($name, false, true);
195
    }
196
197
    /**
198
     * @param string $name of Image Field template
199
     *
200
     * @return string (link)
201
     */
202
    public function PerfectCMSImageLinkRetinaWebP(string $name): string
203
    {
204
        return $this->PerfectCMSImageLink($name, true, true);
205
    }
206
207
    /**
208
     * @param string $name of Image Field template
209
     *
210
     * @return string (link)
211
     */
212
    public function PerfectCMSImageLinkNonRetinaForMobile(string $name): string
213
    {
214
        return $this->PerfectCMSImageLink($name, false, false, true);
215
    }
216
217
    /**
218
     * @param string $name of Image Field template
219
     *
220
     * @return string (link)
221
     */
222
    public function PerfectCMSImageLinkRetinaForMobile(string $name): string
223
    {
224
        return $this->PerfectCMSImageLink($name, true, false, true);
225
    }
226
227
    /**
228
     * @param string $name of Image Field template
229
     *
230
     * @return string (link)
231
     */
232
    public function PerfectCMSImageLinkNonRetinaWebPForMobile(string $name): string
233
    {
234
        return $this->PerfectCMSImageLink($name, false, true, true);
235
    }
236
237
    /**
238
     * @param string $name of Image Field template
239
     *
240
     * @return string (link)
241
     */
242
    public function PerfectCMSImageLinkRetinaWebPForMobile(string $name): string
243
    {
244
        return $this->PerfectCMSImageLink($name, true, true, true);
245
    }
246
247
    /**
248
     * @return string (link)
249
     */
250
    public function getPerfectCMSImageAbsoluteLink(string $link): string
251
    {
252
        return Director::absoluteURL($link);
253
    }
254
255
    /**
256
     * returns image link (if any).
257
     */
258
    public function PerfectCMSImageLink(string $name, ?bool $useRetina = false, ?bool $isWebP = false, ?bool $forMobile = false): string
259
    {
260
        /** @var null|Image $image */
261
        $image = $this->owner;
262
        $allOk = false;
263
        if ($image && $image->exists() && $image instanceof Image) {
264
            $allOk = true;
265
            //we are all good ...
266
        } else {
267
            $image = ImageManipulations::get_backup_image($name);
268
            if ($image && $image->exists() && $image instanceof Image) {
269
                $allOk = true;
270
            }
271
        }
272
273
        if ($allOk) {
274
            // $backEndString = Image::get_backend();
275
            // $backend = Injector::inst()->get($backEndString);
276
            $link = ImageManipulations::get_image_link($image, $name, $useRetina, $forMobile);
277
278
            if ($isWebP) {
279
                $link = ImageManipulations::web_p_link($link);
280
            }
281
282
            return $link ? ImageManipulations::add_fake_parts($image, $link) : '';
283
        } else {
284
            // no image -> provide placeholder if in DEV MODE only!!!
285
            if (Director::isDev()) {
286
                return ImageManipulations::get_placeholder_image_tag($name);
287
            }
288
        }
289
290
        // no image -> provide placeholder if in DEV MODE only!!!
291
        if (Director::isDev()) {
292
            return ImageManipulations::get_placeholder_image_tag($name);
293
        }
294
295
        return '';
296
    }
297
298
    public function PerfectCMSImageFixFolder($name, ?string $folderName = ''): ?Folder
299
    {
300
        $folder = null;
301
        if (PerfectCMSImages::move_to_right_folder($name) || $folderName) {
302
            $image = $this->getOwner();
303
            if ($image) {
0 ignored issues
show
introduced by
$image is of type object, thus it always evaluated to true.
Loading history...
304
                if (! $folderName) {
305
                    $folderName = PerfectCMSImages::get_folder($name);
306
                }
307
                $folder = Folder::find_or_make($folderName);
308
                if (! $folder->ID) {
309
                    $folder->write();
310
                }
311
                if ($image->ParentID !== $folder->ID) {
312
                    $wasPublished = $image->isPublished() && ! $image->isModifiedOnDraft();
313
                    $image->ParentID = $folder->ID;
314
                    $image->write();
315
                    if ($wasPublished) {
316
                        $image->publishRecursive();
317
                    }
318
                }
319
            }
320
            // user_error('could not find image');
321
        }
322
323
        return $folder;
324
    }
325
326
    public function getThumbnail()
327
    {
328
        if ($this->owner->ID) {
329
            if ('svg' === $this->owner->getExtension()) {
330
                $obj = DBHTMLText::create();
331
                $obj->setValue(file_get_contents(BASE_PATH . $this->owner->Link()));
332
333
                return $obj;
334
            }
335
336
            return $this->owner->CMSThumbnail();
337
        }
338
339
        return $this->owner->CMSThumbnail();
340
    }
341
342
    public function updatePreviewLink(&$link, $action)
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

342
    public function updatePreviewLink(&$link, /** @scrutinizer ignore-unused */ $action)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
343
    {
344
        $owner = $this->getOwner();
345
        if ('svg' === $this->owner->getExtension()) {
346
            return $owner->Link();
347
        }
348
349
        return $link;
350
    }
351
}
352