ImageHandler   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
eloc 57
c 4
b 0
f 0
dl 0
loc 139
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getImageTransformation() 0 7 2
A getImageTransformationURL() 0 6 1
A handleManyImagesType() 0 16 3
A handleImageType() 0 27 4
A fixExtension() 0 23 3
1
<?php
2
3
namespace Dynamic\Salsify\TypeHandler\Asset;
4
5
use Dynamic\Salsify\Task\ImportTask;
6
use SilverStripe\Assets\Image;
7
use SilverStripe\ORM\DataObject;
8
9
/**
10
 * Class ImageHandler
11
 * @package Dynamic\Salsify\TypeHandler
12
 *
13
 * @property-read \Dynamic\Salsify\Model\Mapper|ImageHandler $owner
14
 */
15
class ImageHandler extends AssetHandler
16
{
17
18
    /**
19
     * @var array
20
     */
21
    private static $field_types = [
0 ignored issues
show
introduced by
The private property $field_types is not used, and could be removed.
Loading history...
22
        'Image' => [
23
            'requiresWrite' => true,
24
            'requiresSalsifyObjects' => false,
25
            'allowsModification' => true,
26
        ],
27
        'ManyImages' => [
28
            'requiresWrite' => true,
29
            'requiresSalsifyObjects' => false,
30
            'allowsModification' => true,
31
        ],
32
    ];
33
34
    /**
35
     * @var string
36
     */
37
    private static $defaultImageType = 'png';
0 ignored issues
show
introduced by
The private property $defaultImageType is not used, and could be removed.
Loading history...
38
39
    /**
40
     * @param string $url
41
     * @param array|string $transformations
42
     */
43
    public function getImageTransformation($url, $transformations)
0 ignored issues
show
Unused Code introduced by
The parameter $url 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

43
    public function getImageTransformation(/** @scrutinizer ignore-unused */ $url, $transformations)

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...
44
    {
45
        if (!is_array($transformations)) {
46
            $transformations = [$transformations];
47
        }
48
49
        return implode(',', $transformations);
50
    }
51
52
    /**
53
     * @param string $url
54
     * @param string $transform
55
     */
56
    public function getImageTransformationURL($url, $transform)
57
    {
58
        $filePath = preg_split('|^(.*[\\\/])|', $url, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)[0];
59
        $fileName = basename($url);
60
61
        return "{$filePath}{$transform}/{$fileName}";
62
    }
63
64
    /**
65
     * @param string|DataObject $class
66
     * @param $data
67
     * @param $dataField
68
     * @param $config
69
     * @param $dbField
70
     * @return string|int
71
     *
72
     * @throws \Exception
73
     */
74
    public function handleImageType($class, $data, $dataField, $config, $dbField)
0 ignored issues
show
Unused Code introduced by
The parameter $class 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

74
    public function handleImageType(/** @scrutinizer ignore-unused */ $class, $data, $dataField, $config, $dbField)

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...
75
    {
76
        $assetData = $this->getAssetBySalsifyID($data[$dataField]);
77
        if (!$assetData) {
78
            return '';
79
        }
80
81
        $url = $this->fixExtension($assetData['salsify:url']);
82
        $name = $this->fixExtension($assetData['salsify:name']);
83
        $transformation = '';
84
85
        if (array_key_exists('transform', $config)) {
86
            $transformation = $this->getImageTransformation($url, $config['transform']);
87
            $url = $this->getImageTransformationURL($url, $transformation);
88
        }
89
90
        $asset = $this->updateFile(
91
            $assetData['salsify:id'],
92
            $assetData['salsify:updated_at'],
93
            $url,
94
            $name,
95
            $config['type'],
96
            Image::class,
97
            $transformation
98
        );
99
100
        return preg_match('/ID$/', $dbField) ? $asset->ID : $asset;
101
    }
102
103
    protected function fixExtension($string)
104
    {
105
        $supportedImageExtensions = Image::get_category_extensions(
106
            Image::singleton()->File->getAllowedCategories()
107
        );
108
109
        $pathinfo = pathinfo($string);
110
        $defualtExtension = $this->owner->config()->get('defaultImageType');
0 ignored issues
show
Bug introduced by
The method config() does not exist on Dynamic\Salsify\TypeHandler\Asset\ImageHandler. ( Ignorable by Annotation )

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

110
        $defualtExtension = $this->owner->/** @scrutinizer ignore-call */ config()->get('defaultImageType');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
        if (!array_key_exists('extension', $pathinfo)) {
0 ignored issues
show
Bug introduced by
It seems like $pathinfo can also be of type string; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, maybe add an additional type check? ( Ignorable by Annotation )

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

111
        if (!array_key_exists('extension', /** @scrutinizer ignore-type */ $pathinfo)) {
Loading history...
112
            return "{$string}.{$defualtExtension}";
113
        }
114
115
        $extension = pathinfo($string)['extension'];
116
117
        if (!in_array($extension, $supportedImageExtensions)) {
118
            return str_replace(
119
                ".{$extension}",
120
                ".{$defualtExtension}",
121
                $string
122
            );
123
        }
124
125
        return $string;
126
    }
127
128
    /**
129
     * @param string|DataObject $class
130
     * @param $data
131
     * @param $dataField
132
     * @param $config
133
     * @param $dbField
134
     * @return array
135
     *
136
     * @throws \Exception
137
     */
138
    public function handleManyImagesType($class, $data, $dataField, $config, $dbField)
139
    {
140
        $files = [];
141
        $fieldData = $data[$dataField];
142
        // convert to array to prevent problems
143
        if (!is_array($fieldData)) {
144
            $fieldData = [$fieldData];
145
        }
146
147
        foreach ($this->owner->yieldSingle($fieldData) as $fileID) {
0 ignored issues
show
Bug introduced by
The method yieldSingle() does not exist on Dynamic\Salsify\TypeHandler\Asset\ImageHandler. ( Ignorable by Annotation )

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

147
        foreach ($this->owner->/** @scrutinizer ignore-call */ yieldSingle($fieldData) as $fileID) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
            $entryData = array_merge($data, [
149
                $dataField => $fileID
150
            ]);
151
            $files[] = $this->owner->handleImageType($class, $entryData, $dataField, $config, $dbField, $class);
0 ignored issues
show
Bug introduced by
The method handleImageType() does not exist on Dynamic\Salsify\Model\Mapper. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

151
            /** @scrutinizer ignore-call */ 
152
            $files[] = $this->owner->handleImageType($class, $entryData, $dataField, $config, $dbField, $class);
Loading history...
Unused Code introduced by
The call to Dynamic\Salsify\TypeHand...dler::handleImageType() has too many arguments starting with $class. ( Ignorable by Annotation )

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

151
            /** @scrutinizer ignore-call */ 
152
            $files[] = $this->owner->handleImageType($class, $entryData, $dataField, $config, $dbField, $class);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
152
        }
153
        return $files;
154
    }
155
}
156