VersionedImageExtension::onBeforeWrite()   C
last analyzed

Complexity

Conditions 15
Paths 26

Size

Total Lines 79

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 5.1914
c 0
b 0
f 0
cc 15
nc 26
nop 0

How to fix   Long Method    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
 * An extension to automatically regenerate all cached/resampled images when an
4
 * image version is changed.
5
 *
6
 * @package silverstripe-versionedfiles
7
 */
8
class VersionedImageExtension extends DataExtension
9
{
10
    /**
11
     * Regenerates all cached images if the version number has been changed.
12
     */
13
    public function onBeforeWrite()
14
    {
15
        if (!$this->owner->isChanged('CurrentVersionID')) {
16
            return;
17
        }
18
19
        // Support new {@see Image::regenerateFormattedImages} method in 3.2
20
        if ($this->owner->hasMethod('regenerateFormattedImages')) {
21
            $this->owner->regenerateFormattedImages();
22
            return;
23
        }
24
25
        if ($this->owner->ParentID) {
0 ignored issues
show
Bug introduced by
The property ParentID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
26
            $base = $this->owner->Parent()->getFullPath();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SS_Object. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
27
        } else {
28
            $base = ASSETS_PATH . '/';
29
        }
30
31
        $resampled = "{$base}_resampled";
32
        if (!is_dir($resampled)) {
33
            return;
34
        }
35
36
        $files    = scandir($resampled);
37
        $iterator = new ArrayIterator($files);
38
        $filter   = new RegexIterator(
39
            $iterator,
40
            sprintf(
41
                "/([a-z]+)([0-9]?[0-9a-f]*)-%s/i",
42
                preg_quote($this->owner->Name)
0 ignored issues
show
Bug introduced by
The property Name does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
43
            ),
44
            RegexIterator::GET_MATCH
45
        );
46
47
        // grab each resampled image and regenerate it
48
        foreach ($filter as $cachedImage) {
49
            $path      = "$resampled/{$cachedImage[0]}";
50
51
            //skip resampled image files that don't exist
52
            if (!file_exists($path)) {
53
                continue;
54
            }
55
56
            $size      = getimagesize($path);
57
            $method    = $cachedImage[1];
58
            $arguments = $cachedImage[2];
59
60
            unlink($path);
61
62
            // Determine the arguments used to generate an image, and regenerate
63
            // it. Different methods need different ways of determining the
64
            // original arguments used.
65
            switch (strtolower($method)) {
66
                case 'paddedimage':
67
                    $color = preg_replace("/^{$size[0]}{$size[1]}/", '', $arguments);
68
                    $this->owner->$method($size[0], $size[1], $color);
69
                    break;
70
                case 'resizedimage':
71
                case 'setsize':
72
                case 'croppedimage':
73
                case 'setratiosize':
74
                case 'croppedfocusedimage':
75
                    $this->owner->$method($size[0], $size[1]);
76
                    break;
77
78
                case 'setwidth':
79
                    $this->owner->$method($size[0]);
80
                    break;
81
82
                case 'setheight':
83
                    $this->owner->$method($size[1]);
84
                    break;
85
86
                default:
87
                    $this->owner->$method($arguments);
88
                    break;
89
            }
90
        }
91
    }
92
}
93