AssetHandler::getAssetBySalsifyID()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 19
rs 9.6111
cc 5
nc 8
nop 1
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->hasExtension(ImageDataExtension::class)) {
0 ignored issues
show
Bug introduced by
The method hasExtension() 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 */ hasExtension(ImageDataExtension::class)) {

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 hasExtension() 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 */ hasExtension(ImageDataExtension::class)) {

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
            /** @var ImageDataExtension|Image|File $file */
140
            $file->Transformation = $transformation;
141
        }
142
        $file->setFromStream(fopen($url, 'r'), $name);
0 ignored issues
show
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

142
        $file->/** @scrutinizer ignore-call */ 
143
               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\SalsifyIDExtension. ( Ignorable by Annotation )

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

142
        $file->/** @scrutinizer ignore-call */ 
143
               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\ImageDataExtension. ( Ignorable by Annotation )

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

142
        $file->/** @scrutinizer ignore-call */ 
143
               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...
143
144
        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\ImageDataExtension 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

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

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