Passed
Pull Request — master (#17)
by Matthew
02:15
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\Config\Config;
9
use SilverStripe\Core\Extension;
10
use SilverStripe\ORM\DataObject;
11
12
/**
13
 * Class AssetHandler
14
 * @package Dynamic\Salsify\TypeHandler\Asset
15
 *
16
 * @property-read \Dynamic\Salsify\TypeHandler\Asset\AssetHandler|\Dynamic\Salsify\Model\Mapper $owner
17
 */
18
class AssetHandler extends Extension
19
{
20
    /**
21
     * @param $id
22
     * @return array
23
     * @throws \Exception
24
     */
25
    protected function fetchAsset($id)
26
    {
27
        $apiKey = Config::inst()->get(Fetcher::class, 'apiKey');
28
        $timeout = Config::inst()->get(Fetcher::class, 'timeout');
29
        $orgID = Config::inst()->get(Fetcher::class, 'organizationID');
30
31
        $url = "v1/orgs/{$orgID}/digital_assets/{$id}";
32
33
        $client = new Client([
34
            'base_uri' => Fetcher::API_BASE_URL,
35
            'timeout' => $timeout,
36
            'http_errors' => false,
37
            'verify' => true,
38
            'headers' => [
39
                'Authorization' => 'Bearer ' . $apiKey,
40
                'Content-Type' => 'application/json',
41
            ],
42
        ]);
43
44
        $response = $client->get($url);
45
        return json_decode($response->getBody(), true);
46
    }
47
48
    /**
49
     * @param $id
50
     * @return array|bool
51
     * @throws \Exception
52
     */
53
    protected function getAssetBySalsifyID($id)
54
    {
55
        if (is_array($id)) {
56
            $id = $id[count($id) - 1];
57
        }
58
59
        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

59
        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...
60
            return $this->fetchAsset($id);
61
        }
62
63
        $asset = false;
64
        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

64
        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...
65
            if ($data['salsify:id'] == $id) {
66
                $asset = $data;
67
            }
68
        }
69
        $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
        $this->owner->/** @scrutinizer ignore-call */ 
70
                      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...
70
        return $asset;
71
    }
72
73
    /**
74
     * @param string $id
75
     * @param string|DataObject $class
76
     * @return File|\Dyanmic\Salsify\ORM\SalsifyIDExtension
77
     */
78
    protected function findOrCreateFile($id, $class = File::class)
79
    {
80
        /** @var File|\Dyanmic\Salsify\ORM\SalsifyIDExtension $file */
81
        if ($file = $class::get()->find('SalisfyID', $id)) {
82
            return $file;
83
        }
84
85
        $file = $class::create();
86
        $file->SalsifyID = $id;
87
        return $file;
88
    }
89
90
    /**
91
     * @param int|string $id
92
     * @param string $updatedAt
93
     * @param string $url
94
     * @param string $name
95
     * @param string|DataObject $class
96
     *
97
     * @return File|bool
98
     * @throws \Exception
99
     */
100
    protected function updateFile($id, $updatedAt, $url, $name, $class = File::class)
101
    {
102
        $file = $this->findOrCreateFile($id, $class);
103
        if ($file->SalsifyUpdatedAt && $file->SalsifyUpdatedAt == $updatedAt) {
104
            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...
105
        }
106
107
        $file->SalsifyUpdatedAt = $updatedAt;
108
        $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

108
        $file->/** @scrutinizer ignore-call */ 
109
               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...
109
110
        $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

110
        $file->/** @scrutinizer ignore-call */ 
111
               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...
111
        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...
112
    }
113
}
114