Passed
Push — master ( f45a11...606bf7 )
by Matthew
01:41
created

AssetHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 21
c 1
b 0
f 0
dl 0
loc 61
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateFile() 0 12 3
A findOrCreateFile() 0 10 2
A getAssetBySalsifyID() 0 14 4
1
<?php
2
3
namespace Dynamic\Salsify\TypeHandler\Asset;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Core\Extension;
7
use SilverStripe\ORM\DataObject;
8
9
/**
10
 * Class AssetHandler
11
 * @package Dynamic\Salsify\TypeHandler\Asset
12
 */
13
class AssetHandler extends Extension
14
{
15
    /**
16
     * @param $id
17
     * @return array|bool
18
     */
19
    protected function getAssetBySalsifyID($id)
20
    {
21
        if (is_array($id)) {
22
            $id = $id[count($id) - 1];
23
        }
24
25
        $asset = false;
26
        foreach ($this->owner->getAssetStream() as $name => $data) {
27
            if ($data['salsify:id'] == $id) {
28
                $asset = $data;
29
            }
30
        }
31
        $this->owner->resetAssetStream();
32
        return $asset;
33
    }
34
35
    /**
36
     * @param string $id
37
     * @param string|DataObject $class
38
     * @return File|\Dyanmic\Salsify\ORM\FileExtension
39
     */
40
    protected function findOrCreateFile($id, $class = File::class)
41
    {
42
        /** @var File|\Dyanmic\Salsify\ORM\FileExtension $file */
43
        if ($file = $class::get()->find('SalisfyID', $id)) {
44
            return $file;
45
        }
46
47
        $file = $class::create();
48
        $file->SalisfyID = $id;
49
        return $file;
50
    }
51
52
    /**
53
     * @param int|string $id
54
     * @param string $updatedAt
55
     * @param string $url
56
     * @param string $name
57
     * @param string|DataObject $class
58
     *
59
     * @return File|bool
60
     * @throws \Exception
61
     */
62
    protected function updateFile($id, $updatedAt, $url, $name, $class = File::class)
63
    {
64
        $file = $this->findOrCreateFile($id, $class);
65
        if ($file->SalsifyUpdatedAt && $file->SalsifyUpdatedAt == $updatedAt) {
66
            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...
67
        }
68
69
        $file->SalsifyUpdatedAt = $updatedAt;
70
        $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

70
        $file->/** @scrutinizer ignore-call */ 
71
               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...
71
72
        $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

72
        $file->/** @scrutinizer ignore-call */ 
73
               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...
73
        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...
74
    }
75
}
76