FileHandler::handleFileType()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
rs 9.9332
cc 3
nc 3
nop 5
1
<?php
2
3
namespace Dynamic\Salsify\TypeHandler\Asset;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\ORM\DataObject;
7
8
/**
9
 * Class FileHandler
10
 * @package Dynamic\Salsify\TypeHandler
11
 *
12
 * @property-read \Dynamic\Salsify\Model\Mapper|FileHandler $owner
13
 */
14
class FileHandler extends AssetHandler
15
{
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
        'File' => [
22
            'requiresWrite' => true,
23
            'requiresSalsifyObjects' => false,
24
            'allowsModification' => true,
25
        ],
26
        'ManyFiles' => [
27
            'requiresWrite' => true,
28
            'requiresSalsifyObjects' => false,
29
            'allowsModification' => true,
30
        ],
31
    ];
32
33
    /**
34
     * @param string|DataObject $class
35
     * @param $data
36
     * @param $dataField
37
     * @param $config
38
     * @param $dbField
39
     * @return string|int
40
     *
41
     * @throws \Exception
42
     */
43
    public function handleFileType($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

43
    public function handleFileType(/** @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...
44
    {
45
        $data = $this->getAssetBySalsifyID($data[$dataField]);
46
        if (!$data) {
47
            return '';
48
        }
49
50
        $asset = $this->updateFile(
51
            $data['salsify:id'],
52
            $data['salsify:updated_at'],
53
            $data['salsify:url'],
54
            $data['salsify:name'],
55
            $config['type']
56
        );
57
        return preg_match('/ID$/', $dbField) ? $asset->ID : $asset;
58
    }
59
60
    /**
61
     * @param string|DataObject $class
62
     * @param $data
63
     * @param $dataField
64
     * @param $config
65
     * @param $dbField
66
     * @return array
67
     *
68
     * @throws \Exception
69
     */
70
    public function handleManyFilesType($class, $data, $dataField, $config, $dbField)
71
    {
72
        $files = [];
73
        $fieldData = $data[$dataField];
74
        // convert to array to prevent problems
75
        if (!is_array($fieldData)) {
76
            $fieldData = [$fieldData];
77
        }
78
79
        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\FileHandler. ( Ignorable by Annotation )

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

79
        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...
80
            $entryData = array_merge($data, [
81
                $dataField => $fileID
82
            ]);
83
            $files[] = $this->owner->handleFileType($class, $entryData, $dataField, $config, $dbField, $class);
0 ignored issues
show
Bug introduced by
The method handleFileType() 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

83
            /** @scrutinizer ignore-call */ 
84
            $files[] = $this->owner->handleFileType($class, $entryData, $dataField, $config, $dbField, $class);
Loading history...
Unused Code introduced by
The call to Dynamic\Salsify\TypeHand...ndler::handleFileType() 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

83
            /** @scrutinizer ignore-call */ 
84
            $files[] = $this->owner->handleFileType($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...
84
        }
85
        return $files;
86
    }
87
}
88