Passed
Push — master ( f45a11...606bf7 )
by Matthew
01:41
created

ImageHandler::handleImageType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 18
rs 9.8666
cc 3
nc 3
nop 5
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|\Dynamic\Salsify\TypeHandler\Asset\ImageHandler $owner
14
 */
15
class ImageHandler extends AssetHandler
16
{
17
    /**
18
     * @var array
19
     */
20
    private static $field_types = [
0 ignored issues
show
introduced by
The private property $field_types is not used, and could be removed.
Loading history...
21
        'Image',
22
        'ManyImages',
23
    ];
24
25
    /**
26
     * @var string
27
     */
28
    private static $defaultImageType = 'png';
0 ignored issues
show
introduced by
The private property $defaultImageType is not used, and could be removed.
Loading history...
29
30
    /**
31
     * @param $data
32
     * @param $dataField
33
     * @param $config
34
     * @param $dbField
35
     * @param string |DataObject $class
36
     * @return string|int
37
     *
38
     * @throws \Exception
39
     */
40
    public function handleImageType($data, $dataField, $config, $dbField, $class)
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

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

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...
Unused Code introduced by
The parameter $config 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

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

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...
41
    {
42
        $assetData = $this->getAssetBySalsifyID($data[$dataField]);
43
        if (!$assetData) {
0 ignored issues
show
introduced by
The condition $assetData is always false.
Loading history...
44
            return '';
45
        }
46
47
        $url = $this->fixExtension($assetData['salsify:url']);
48
        $name = $this->fixExtension($assetData['salsify:name']);
49
50
        $asset = $this->updateFile(
51
            $assetData['salsify:id'],
52
            $assetData['salsify:updated_at'],
53
            $url,
54
            $name,
55
            Image::class
56
        );
57
        return preg_match('/ID$/', $dbField) ? $asset->ID : $asset;
58
    }
59
60
    protected function fixExtension($string)
61
    {
62
        $supportedImageExtensions = Image::get_category_extensions(
63
            Image::singleton()->File->getAllowedCategories()
64
        );
65
        $extension = pathinfo($string)['extension'];
66
67
        if (!in_array($extension, $supportedImageExtensions)) {
68
            return str_replace(
69
                '.' . $extension,
70
                '.' . $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

70
                '.' . $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...
71
                $string
72
            );
73
        }
74
75
        return $string;
76
    }
77
78
    /**
79
     * @param $data
80
     * @param $dataField
81
     * @param $config
82
     * @param $dbField
83
     * @param string |DataObject $class
84
     * @return array
85
     *
86
     * @throws \Exception
87
     */
88
    public function handleManyImagesType($data, $dataField, $config, $dbField, $class)
89
    {
90
        $files = [];
91
        $fieldData = $data[$dataField];
92
        foreach ($fieldData as $fileID) {
93
            $entryData = array_merge($data, [
94
                $dataField => $fileID
95
            ]);
96
            $files[] = $this->owner->handleImageType($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

96
            /** @scrutinizer ignore-call */ 
97
            $files[] = $this->owner->handleImageType($entryData, $dataField, $config, $dbField, $class);
Loading history...
97
        }
98
        return $files;
99
    }
100
}
101