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

FileHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateFile() 0 12 3
A findOrCreateFile() 0 10 2
A handleFileType() 0 14 3
A getAssetBySalsifyID() 0 14 4
1
<?php
2
3
namespace Dynamic\Salsify\TypeHandler;
4
5
use JsonMachine\JsonMachine;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Core\Extension;
8
use SilverStripe\ORM\DataObject;
9
10
/**
11
 * Class FileHandler
12
 * @package Dynamic\Salsify\TypeHandler
13
 *
14
 * @property-read \Dynamic\Salsify\Model\Mapper|\Dynamic\Salsify\TypeHandler\FileHandler $owner
15
 */
16
class FileHandler extends Extension
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
        'File'
23
    ];
24
25
    /**
26
     * @var JsonMachine
27
     */
28
    protected $assetStream;
29
30
    /**
31
     * @param $value
32
     * @param $field
33
     * @param string |DataObject $class
34
     * @return string
35
     *
36
     * @throws \Exception
37
     */
38
    public function handleFileType($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

38
    public function handleFileType($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...
39
    {
40
        $data = $this->getAssetBySalsifyID($value);
41
        if (!$data) {
0 ignored issues
show
introduced by
The condition $data is always false.
Loading history...
42
            return '';
43
        }
44
45
        $asset = $this->updateFile(
46
            $data['salsify:id'],
47
            $data['salsify:updated_at'],
48
            $data['salsify:url'],
49
            $data['salsify:name']
50
        );
51
        return preg_match('/ID$/', $field) ? $asset->ID : $asset;
52
    }
53
54
    /**
55
     * @param $id
56
     * @return array|bool
57
     */
58
    protected function getAssetBySalsifyID($id)
59
    {
60
        if (is_array($id)) {
61
            $id = $id[count($id) - 1];
62
        }
63
64
        $asset = false;
65
        foreach ($this->owner->getAssetStream() as $name => $data) {
0 ignored issues
show
Bug introduced by
The method getAssetStream() does not exist on Dynamic\Salsify\TypeHandler\FileHandler. ( Ignorable by Annotation )

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

65
        foreach ($this->owner->/** @scrutinizer ignore-call */ getAssetStream() as $name => $data) {

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...
66
            if ($data['salsify:id'] == $id) {
67
                $asset = $data;
68
            }
69
        }
70
        $this->owner->resetAssetStream();
0 ignored issues
show
Bug introduced by
The method resetAssetStream() does not exist on Dynamic\Salsify\TypeHandler\FileHandler. ( 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 */ 
71
                      resetAssetStream();

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
        return $asset;
72
    }
73
74
    /**
75
     * @param string $id
76
     * @param string|DataObject $class
77
     * @return File|\Dyanmic\Salsify\ORM\FileExtension
78
     */
79
    protected function findOrCreateFile($id, $class = File::class)
80
    {
81
        /** @var File|\Dyanmic\Salsify\ORM\FileExtension $file */
82
        if ($file = $class::get()->find('SalisfyID', $id)) {
83
            return $file;
84
        }
85
86
        $file = $class::create();
87
        $file->SalisfyID = $id;
88
        return $file;
89
    }
90
91
    /**
92
     * @param int|string $id
93
     * @param string $updatedAt
94
     * @param string $url
95
     * @param string $name
96
     * @param string|DataObject $class
97
     *
98
     * @return File|bool
99
     * @throws \Exception
100
     */
101
    protected function updateFile($id, $updatedAt, $url, $name, $class = File::class)
102
    {
103
        $file = $this->findOrCreateFile($id, $class);
104
        if ($file->SalsifyUpdatedAt && $file->SalsifyUpdatedAt == $updatedAt) {
105
            return $file;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $file also could return the type Dyanmic\Salsify\ORM\FileExtension which is incompatible with the documented return type SilverStripe\Assets\File|boolean.
Loading history...
106
        }
107
108
        $file->SalsifyUpdatedAt = $updatedAt;
109
        $file->setFromStream(fopen($url, 'r'), $name);
0 ignored issues
show
Bug introduced by
The method setFromStream() does not exist on Dyanmic\Salsify\ORM\FileExtension. ( Ignorable by Annotation )

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

109
        $file->/** @scrutinizer ignore-call */ 
110
               setFromStream(fopen($url, 'r'), $name);

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...
110
111
        $file->write();
0 ignored issues
show
Bug introduced by
The method write() does not exist on Dyanmic\Salsify\ORM\FileExtension. ( Ignorable by Annotation )

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

111
        $file->/** @scrutinizer ignore-call */ 
112
               write();

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...
112
        return $file;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $file also could return the type Dyanmic\Salsify\ORM\FileExtension which is incompatible with the documented return type SilverStripe\Assets\File|boolean.
Loading history...
113
    }
114
}
115