Completed
Pull Request — master (#114)
by Bart
07:39
created

AssetTransforms::getRecords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft;
6
use craft\models\AssetTransform;
7
8
/**
9
 * Schematic Asset Transforms Service.
10
 *
11
 * Sync Craft Setups.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2018, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @see      http://www.nerds.company
18
 */
19
class AssetTransforms extends Base
20
{
21
    //==============================================================================================================
22
    //================================================  EXPORT  ====================================================
23
    //==============================================================================================================
24 4
25
    /**
26 4
     * Get all asset transforms
27
     *
28
     * @return AssetTransform[]
29
     */
30
    protected function getRecords()
31
    {
32
        return Craft::$app->assetTransforms->getAllTransforms();
33
    }
34
35
    //==============================================================================================================
36 3
    //================================================  IMPORT  ====================================================
37
    //==============================================================================================================
38 3
39
    /**
40 3
     * Import asset transform definitions.
41
     *
42 3
     * @param array $assetTransformDefinitions
43 2
     * @param bool  $force
44 3
     *
45
     * @return Result
46 3
     */
47
    public function import($force = false, array $assetTransformDefinitions = null)
48
    {
49
        Craft::info('Importing Asset Transforms', 'schematic');
50
51
        $this->resetCraftAssetTransformsServiceCache();
52
        $assetTransforms = $this->getAssetTransformsService()->getAllTransforms('handle');
0 ignored issues
show
Documentation Bug introduced by
The method getAssetTransformsService does not exist on object<NerdsAndCompany\S...rvices\AssetTransforms>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
53
54 2
        foreach ($assetTransformDefinitions as $assetTransformHandle => $assetTransformDefinition) {
0 ignored issues
show
Bug introduced by
The expression $assetTransformDefinitions of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
55
            $assetTransform = array_key_exists($assetTransformHandle, $assetTransforms)
56
                ? $assetTransforms[$assetTransformHandle]
57 2
                : new AssetTransformModel();
58 2
59 2
            unset($assetTransforms[$assetTransformHandle]);
60 2
61 2
            $this->populateAssetTransform($assetTransform, $assetTransformDefinition, $assetTransformHandle);
62 2
63 2
            if (!$this->getAssetTransformsService()->saveTransform($assetTransform)) { // Save asset transform via craft
0 ignored issues
show
Documentation Bug introduced by
The method getAssetTransformsService does not exist on object<NerdsAndCompany\S...rvices\AssetTransforms>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
64 2
                $this->addErrors($assetTransform->getAllErrors());
0 ignored issues
show
Documentation Bug introduced by
The method addErrors does not exist on object<NerdsAndCompany\S...rvices\AssetTransforms>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
65
66
                continue;
67
            }
68
        }
69
70
        if ($force) {
71
            foreach ($assetTransforms as $assetTransform) {
72
                $this->getAssetTransformsService()->deleteTransform($assetTransform->id);
0 ignored issues
show
Documentation Bug introduced by
The method getAssetTransformsService does not exist on object<NerdsAndCompany\S...rvices\AssetTransforms>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
73
            }
74
        }
75 4
76
        return $this->getResultModel();
0 ignored issues
show
Documentation Bug introduced by
The method getResultModel does not exist on object<NerdsAndCompany\S...rvices\AssetTransforms>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
77 4
    }
78
79 4
    /**
80 4
     * Populate asset transform.
81
     *
82 4
     * @param AssetTransformModel $assetTransform
83 2
     * @param array               $assetTransformDefinition
84 2
     * @param string              $assetTransformHandle
85 2
     *
86
     * @return AssetTransformModel
87 2
     */
88
    private function populateAssetTransform(AssetTransformModel $assetTransform, array $assetTransformDefinition, $assetTransformHandle)
89 2
    {
90
        $assetTransform->setAttributes([
91 2
            'handle' => $assetTransformHandle,
92 2
            'name' => $assetTransformDefinition['name'],
93
            'width' => $assetTransformDefinition['width'],
94 2
            'height' => $assetTransformDefinition['height'],
95
            'format' => $assetTransformDefinition['format'],
96 4
            'mode' => $assetTransformDefinition['mode'],
97
            'position' => $assetTransformDefinition['position'],
98 4
            'quality' => $assetTransformDefinition['quality'],
99 2
        ]);
100
101 2
        return $assetTransform;
102 2
    }
103
104 4
    /**
105
     * Reset craft fields service cache using reflection.
106
     */
107
    private function resetCraftAssetTransformsServiceCache()
108
    {
109
        $obj = $this->getAssetTransformsService();
0 ignored issues
show
Documentation Bug introduced by
The method getAssetTransformsService does not exist on object<NerdsAndCompany\S...rvices\AssetTransforms>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
110
        $refObject = new \ReflectionObject($obj);
111
        if ($refObject->hasProperty('_fetchedAllTransforms')) {
112
            $refProperty = $refObject->getProperty('_fetchedAllTransforms');
113
            $refProperty->setAccessible(true);
114
            $refProperty->setValue($obj, false);
115
        }
116 2
        if ($refObject->hasProperty('_transformsByHandle')) {
117
            $refProperty = $refObject->getProperty('_transformsByHandle');
118 2
            $refProperty->setAccessible(true);
119 2
            $refProperty->setValue($obj, array());
120 2
        }
121 2
    }
122
}
123