Passed
Pull Request — master (#16)
by Matthew
02:08
created

ImageHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 25
c 0
b 0
f 0
dl 0
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleImageType() 0 18 3
A fixExtension() 0 16 2
1
<?php
2
3
namespace Dynamic\Salsify\TypeHandler\Asset;
4
5
use SilverStripe\Assets\Image;
6
use SilverStripe\ORM\DataObject;
7
8
/**
9
 * Class ImageHandler
10
 * @package Dynamic\Salsify\TypeHandler
11
 *
12
 * @property-read \Dynamic\Salsify\Model\Mapper|\Dynamic\Salsify\TypeHandler\Asset\ImageHandler $owner
13
 */
14
class ImageHandler extends AssetHandler
15
{
16
    /**
17
     * @var array
18
     */
19
    private static $field_types = [
0 ignored issues
show
introduced by
The private property $field_types is not used, and could be removed.
Loading history...
20
        'Image'
21
    ];
22
23
    /**
24
     * @var string
25
     */
26
    private static $defaultImageType = 'png';
0 ignored issues
show
introduced by
The private property $defaultImageType is not used, and could be removed.
Loading history...
27
28
    /**
29
     * @param $data
30
     * @param $dataField
31
     * @param $config
32
     * @param $dbField
33
     * @param string |DataObject $class
34
     * @return string|int
35
     *
36
     * @throws \Exception
37
     */
38
    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

38
    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

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

68
                '.' . $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...
69
                $string
70
            );
71
        }
72
73
        return $string;
74
    }
75
}
76