Passed
Push — master ( 606bf7...dde9c0 )
by Matthew
01:54
created

AssetHandler::fetchAsset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 21
rs 9.7998
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Dynamic\Salsify\TypeHandler\Asset;
4
5
use Dynamic\Salsify\Model\Fetcher;
6
use GuzzleHttp\Client;
7
use SilverStripe\Assets\File;
8
use SilverStripe\Core\Extension;
9
use SilverStripe\ORM\DataObject;
10
11
/**
12
 * Class AssetHandler
13
 * @package Dynamic\Salsify\TypeHandler\Asset
14
 *
15
 * @property-read \Dynamic\Salsify\TypeHandler\Asset\AssetHandler|\Dynamic\Salsify\Model\Mapper $owner
16
 */
17
class AssetHandler extends Extension
18
{
19
    /**
20
     * @param $id
21
     * @return array
22
     * @throws \Exception
23
     */
24
    protected function fetchAsset($id)
25
    {
26
        $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

26
        $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...
27
        $timeout = $this->owner->config()->get('timeout');
28
        $orgID = $this->owner->config()->get('organizationID');
29
30
        $url = "v1/orgs/{$orgID}/digital_assets/{$id}";
31
32
        $client = new Client([
33
            'base_uri' => Fetcher::API_BASE_URL,
34
            'timeout' => $timeout,
35
            'http_errors' => false,
36
            'verify' => true,
37
            'headers' => [
38
                'Authorization' => 'Bearer ' . $apiKey,
39
                'Content-Type' => 'application/json',
40
            ],
41
        ]);
42
43
        $response = $client->get($url);
44
        return json_decode($response->getBody(), true);
45
    }
46
47
    /**
48
     * @param $id
49
     * @return array|bool
50
     * @throws \Exception
51
     */
52
    protected function getAssetBySalsifyID($id)
53
    {
54
        if (is_array($id)) {
55
            $id = $id[count($id) - 1];
56
        }
57
58
        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

58
        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...
59
            return $this->fetchAsset($id);
60
        }
61
62
        $asset = false;
63
        foreach ($this->owner->getAssetStream() as $name => $data) {
0 ignored issues
show
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

63
        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...
64
            if ($data['salsify:id'] == $id) {
65
                $asset = $data;
66
            }
67
        }
68
        $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

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

107
        $file->/** @scrutinizer ignore-call */ 
108
               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...
108
109
        $file->write();
0 ignored issues
show
Bug introduced by
The method write() does not exist on Dyanmic\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

109
        $file->/** @scrutinizer ignore-call */ 
110
               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...
110
        return $file;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $file also could return the type Dyanmic\Salsify\ORM\SalsifyIDExtension which is incompatible with the documented return type SilverStripe\Assets\File|boolean.
Loading history...
111
    }
112
}
113