Passed
Push — master ( 096426...b47af5 )
by Matthew
04:41 queued 02:32
created

AssetHandler   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 20
eloc 59
c 2
b 0
f 0
dl 0
loc 155
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isTransformOutOfDate() 0 8 2
A updateFile() 0 16 5
A findOrCreateFile() 0 29 5
A getAssetBySalsifyID() 0 19 5
A fetchAsset() 0 21 1
A writeFile() 0 10 2
1
<?php
2
3
namespace Dynamic\Salsify\TypeHandler\Asset;
4
5
use Dynamic\Salsify\Model\Fetcher;
6
use Dynamic\Salsify\ORM\FileDataExtension;
7
use Dynamic\Salsify\ORM\ImageDataExtension;
8
use Dynamic\Salsify\ORM\SalsifyIDExtension;
9
use Dynamic\Salsify\Traits\Yieldable;
10
use GuzzleHttp\Client;
11
use SilverStripe\Assets\File;
12
use SilverStripe\Assets\Image;
13
use SilverStripe\Core\Extension;
14
use SilverStripe\ORM\DataObject;
15
16
/**
17
 * Class AssetHandler
18
 * @package Dynamic\Salsify\TypeHandler\Asset
19
 *
20
 * @property-read \Dynamic\Salsify\Model\Mapper|AssetHandler $owner
21
 */
22
class AssetHandler extends Extension
23
{
24
25
    /**
26
     * @param $id
27
     * @return array
28
     * @throws \Exception
29
     */
30
    protected function fetchAsset($id)
31
    {
32
        $apiKey = $this->owner->config()->get('apiKey');//Config::inst()->get(Fetcher::class, 'apiKey');
0 ignored issues
show
Bug introduced by
The method config() does not exist on Dynamic\Salsify\TypeHandler\Asset\AssetHandler. ( Ignorable by Annotation )

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

32
        $apiKey = $this->owner->/** @scrutinizer ignore-call */ config()->get('apiKey');//Config::inst()->get(Fetcher::class, 'apiKey');

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...
33
        $timeout = $this->owner->config()->get('timeout');
34
        $orgID = $this->owner->config()->get('organizationID');
35
36
        $url = "v1/orgs/{$orgID}/digital_assets/{$id}";
37
38
        $client = new Client([
39
            'base_uri' => Fetcher::API_BASE_URL,
40
            'timeout' => $timeout,
41
            'http_errors' => false,
42
            'verify' => true,
43
            'headers' => [
44
                'Authorization' => 'Bearer ' . $apiKey,
45
                'Content-Type' => 'application/json',
46
            ],
47
        ]);
48
49
        $response = $client->get($url);
50
        return json_decode($response->getBody(), true);
51
    }
52
53
    /**
54
     * @param $id
55
     * @return array|bool
56
     * @throws \Exception
57
     */
58
    protected function getAssetBySalsifyID($id)
59
    {
60
        if (is_array($id)) {
61
            $id = $id[0];
62
        }
63
64
        if ($this->owner->hasFile() === false) {
0 ignored issues
show
Bug introduced by
The method hasFile() does not exist on Dynamic\Salsify\TypeHandler\Asset\AssetHandler. ( Ignorable by Annotation )

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

64
        if ($this->owner->/** @scrutinizer ignore-call */ hasFile() === false) {

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...
65
            return $this->fetchAsset($id);
66
        }
67
68
        $asset = false;
69
        $assetGenerator = $this->owner->yieldKeyVal($this->owner->getAssetStream(), $this->owner->resetAssetStream());
0 ignored issues
show
Bug introduced by
The method resetAssetStream() does not exist on Dynamic\Salsify\TypeHandler\Asset\AssetHandler. ( Ignorable by Annotation )

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

69
        $assetGenerator = $this->owner->yieldKeyVal($this->owner->getAssetStream(), $this->owner->/** @scrutinizer ignore-call */ 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...
Bug introduced by
The method yieldKeyVal() does not exist on Dynamic\Salsify\TypeHandler\Asset\AssetHandler. ( Ignorable by Annotation )

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

69
        /** @scrutinizer ignore-call */ 
70
        $assetGenerator = $this->owner->yieldKeyVal($this->owner->getAssetStream(), $this->owner->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...
Bug introduced by
The method getAssetStream() does not exist on Dynamic\Salsify\TypeHandler\Asset\AssetHandler. ( Ignorable by Annotation )

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

69
        $assetGenerator = $this->owner->yieldKeyVal($this->owner->/** @scrutinizer ignore-call */ getAssetStream(), $this->owner->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...
Bug introduced by
Are you sure the usage of $this->owner->resetAssetStream() targeting Dynamic\Salsify\Model\Mapper::resetAssetStream() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
70
        foreach ($assetGenerator as $name => $data) {
71
            if ($data['salsify:id'] == $id) {
72
                $asset = $data;
73
                $assetGenerator->send(Yieldable::$STOP_GENERATOR);
74
            }
75
        }
76
        return $asset;
77
    }
78
79
    /**
80
     * @param string $id
81
     * @param string $type
82
     * @param string|DataObject $class
83
     * @return File|SalsifyIDExtension|FileDataExtension
84
     */
85
    protected function findOrCreateFile($id, $type, $class = File::class)
86
    {
87
        $filter = [
88
            'SalsifyID' => $id,
89
            'Type' => $type,
90
        ];
91
        /** @var File|SalsifyIDExtension|FileDataExtension $file */
92
        if ($file = $class::get()->filter($filter)->first()) {
93
            return $file;
94
        }
95
96
        // TODO - remove at a later date
97
        $filter = [
98
            'SalsifyID' => $id,
99
            'Type' => null,
100
        ];
101
        if ($file = $class::get()->filter($filter)->first()) {
102
            // checks for changes from image to file before trying to update
103
            if (!($class === File::class && $file->getClassName() !== File::class)) {
104
                $file->Type = $type;
105
                return $this->writeFile($file);
106
            }
107
        }
108
        // end of TODO removal
109
110
        $file = $class::create();
111
        $file->SalsifyID = $id;
112
        $file->Type = $type;
113
        return $file;
114
    }
115
116
    /**
117
     * @param int|string $id
118
     * @param string $updatedAt
119
     * @param string $url
120
     * @param string $name
121
     * @param string $type
122
     * @param string|DataObject $class
123
     * @param string $transformation
124
     *
125
     * @return File|bool
126
     * @throws \Exception
127
     */
128
    protected function updateFile($id, $updatedAt, $url, $name, $type, $class = File::class, $transformation = '')
129
    {
130
        $file = $this->findOrCreateFile($id, $type, $class);
131
        if ($file->SalsifyUpdatedAt && $file->SalsifyUpdatedAt == $updatedAt) {
0 ignored issues
show
Bug introduced by
The property SalsifyUpdatedAt does not seem to exist on Dynamic\Salsify\ORM\FileDataExtension.
Loading history...
132
            if (!$this->isTransformOutOfDate($file, $class, $transformation)) {
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type Dynamic\Salsify\ORM\FileDataExtension and Dynamic\Salsify\ORM\SalsifyIDExtension; however, parameter $file of Dynamic\Salsify\TypeHand...:isTransformOutOfDate() does only seem to accept SilverStripe\ORM\DataObject, maybe add an additional type check? ( Ignorable by Annotation )

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

132
            if (!$this->isTransformOutOfDate(/** @scrutinizer ignore-type */ $file, $class, $transformation)) {
Loading history...
133
                return $file;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $file also could return the type Dynamic\Salsify\ORM\File...\ORM\SalsifyIDExtension which is incompatible with the documented return type SilverStripe\Assets\File|boolean.
Loading history...
134
            }
135
        }
136
137
        $file->SalsifyUpdatedAt = $updatedAt;
138
        if ($file->hasField('Transformation')) {
0 ignored issues
show
Bug introduced by
The method hasField() does not exist on Dynamic\Salsify\ORM\FileDataExtension. ( Ignorable by Annotation )

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

138
        if ($file->/** @scrutinizer ignore-call */ hasField('Transformation')) {

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...
Bug introduced by
The method hasField() does not exist on Dynamic\Salsify\ORM\SalsifyIDExtension. ( Ignorable by Annotation )

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

138
        if ($file->/** @scrutinizer ignore-call */ hasField('Transformation')) {

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...
139
            $file->Transformation = $transformation;
0 ignored issues
show
Bug introduced by
The property Transformation does not seem to exist on Dynamic\Salsify\ORM\FileDataExtension.
Loading history...
Bug introduced by
The property Transformation does not seem to exist on Dynamic\Salsify\ORM\SalsifyIDExtension.
Loading history...
140
        }
141
        $file->setFromStream(fopen($url, 'r'), $name);
0 ignored issues
show
Bug introduced by
The method setFromStream() does not exist on Dynamic\Salsify\ORM\SalsifyIDExtension. ( Ignorable by Annotation )

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

141
        $file->/** @scrutinizer ignore-call */ 
142
               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...
Bug introduced by
The method setFromStream() does not exist on Dynamic\Salsify\ORM\FileDataExtension. ( Ignorable by Annotation )

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

141
        $file->/** @scrutinizer ignore-call */ 
142
               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...
142
143
        return $this->writeFile($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type Dynamic\Salsify\ORM\FileDataExtension and Dynamic\Salsify\ORM\SalsifyIDExtension; however, parameter $file of Dynamic\Salsify\TypeHand...setHandler::writeFile() does only seem to accept SilverStripe\Assets\File, maybe add an additional type check? ( Ignorable by Annotation )

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

143
        return $this->writeFile(/** @scrutinizer ignore-type */ $file);
Loading history...
144
    }
145
146
    /**
147
     * @param DataObject $file
148
     * @param string $class
149
     * @param string $transformation
150
     *
151
     * @return bool
152
     */
153
    private function isTransformOutOfDate($file, $class, $transformation)
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

153
    private function isTransformOutOfDate($file, /** @scrutinizer ignore-unused */ $class, $transformation)

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...
154
    {
155
        if (!$file instanceof Image) {
156
            return false;
157
        }
158
159
        /** @var Image|ImageDataExtension $file */
160
        return $file->Transformation != $transformation;
161
    }
162
163
    /**
164
     * @param File $file
165
     * @return File
166
     */
167
    private function writeFile($file)
168
    {
169
        $published = $file->isPublished();
170
        $file->write();
171
172
        if ($published) {
173
            $file->publishSingle();
174
        }
175
176
        return $file;
177
    }
178
}
179