Passed
Pull Request — master (#16)
by Matthew
01:54
created

ImageHandler::fixExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 16
rs 9.9666
1
<?php
2
3
namespace Dynamic\Salsify\TypeHandler;
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\ImageHandler $owner
13
 */
14
class ImageHandler extends FileHandler
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 $value
30
     * @param $field
31
     * @param string |DataObject $class
32
     * @return string
33
     *
34
     * @throws \Exception
35
     */
36
    public function handleImageType($value, $field, $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

36
    public function handleImageType($value, $field, /** @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...
37
    {
38
        $data = $this->getAssetBySalsifyID($value);
39
        if (!$data) {
0 ignored issues
show
introduced by
The condition $data is always false.
Loading history...
40
            return '';
41
        }
42
43
        $url = $this->fixExtension($data['salsify:url']);
44
        $name = $this->fixExtension($data['salsify:name']);
45
46
        $asset = $this->updateFile(
47
            $data['salsify:id'],
48
            $data['salsify:updated_at'],
49
            $url,
50
            $name,
51
            Image::class
52
        );
53
        return preg_match('/ID$/', $field) ? $asset->ID : $asset;
54
    }
55
56
    protected function fixExtension($string)
57
    {
58
        $supportedImageExtensions = Image::get_category_extensions(
59
            Image::singleton()->File->getAllowedCategories()
60
        );
61
        $extension = pathinfo($string)['extension'];
62
63
        if (!in_array($extension, $supportedImageExtensions)) {
64
            return str_replace(
65
                '.' . $extension,
66
                '.' . $this->owner->config()->get('defaultImageType'),
0 ignored issues
show
Bug introduced by
The method config() does not exist on Dynamic\Salsify\TypeHandler\ImageHandler. ( Ignorable by Annotation )

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

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