Completed
Pull Request — master (#91)
by Bob Olde
04:31
created

AssetSources::import()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.288

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 16
cts 20
cp 0.8
rs 8.439
c 0
b 0
f 0
cc 6
eloc 17
nc 10
nop 2
crap 6.288
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Services;
4
5
use Craft\Craft;
6
use Craft\AssetSourceModel;
7
8
/**
9
 * Schematic Assets Service.
10
 *
11
 * Sync Craft Setups.
12
 *
13
 * @author    Nerds & Company
14
 * @copyright Copyright (c) 2015-2017, Nerds & Company
15
 * @license   MIT
16
 *
17
 * @link      http://www.nerds.company
18
 */
19
class AssetSources extends Base
20
{
21
    /**
22
     * @return AssetSourcesService
23
     */
24 4
    private function getAssetSourcesService()
25
    {
26 4
        return Craft::app()->assetSources;
27
    }
28
29
    /**
30
     * Export all asset sources.
31
     *
32
     * @param AssetSourceModel[] $assetSources
33
     *
34
     * @return array
35
     */
36 3
    public function export(array $assetSources = [])
37
    {
38 3
        Craft::log(Craft::t('Exporting Asset Sources'));
39
40 3
        $assetSourceDefinitions = [];
41
42 3
        foreach ($assetSources as $assetSource) {
43 2
            $assetSourceDefinitions[$assetSource->handle] = $this->getAssetSourceDefinition($assetSource);
44 1
        }
45
46 1
        return $assetSourceDefinitions;
47
    }
48
49
    /**
50
     * @param AssetSourceModel $assetSource
51
     *
52
     * @return array
53
     */
54 2
    private function getAssetSourceDefinition(AssetSourceModel $assetSource)
55
    {
56
        return [
57 2
            'type' => $assetSource->type,
58 2
            'name' => $assetSource->name,
59 2
            'sortOrder' => $assetSource->sortOrder,
60 2
            'settings' => $assetSource->settings,
61 2
            'fieldLayout' => Craft::app()->schematic_fields->getFieldLayoutDefinition($assetSource->getFieldLayout()),
62
        ];
63
    }
64
65
    /**
66
     * Import asset source definitions.
67
     *
68
     * @param array $assetSourceDefinitions
69
     * @param bool  $force
70
     *
71
     * @return Result
72
     */
73 4
    public function import(array $assetSourceDefinitions, $force = false)
74
    {
75 4
        Craft::log(Craft::t('Importing Asset Sources'));
76
77 4
        $this->resetCraftAssetSourcesServiceCache();
78 4
        $assetSources = $this->getAssetSourcesService()->getAllSources('handle');
79
80 4
        foreach ($assetSourceDefinitions as $assetSourceHandle => $assetSourceDefinition) {
81 2
            $assetSource = array_key_exists($assetSourceHandle, $assetSources)
82 2
                ? $assetSources[$assetSourceHandle]
83 2
                : new AssetSourceModel();
84
85 2
            unset($assetSources[$assetSourceHandle]);
86
87 2
            $this->populateAssetSource($assetSource, $assetSourceDefinition, $assetSourceHandle);
88
89
            if (!$this->getAssetSourcesService()->saveSource($assetSource)) { // Save assetsource via craft
90
                $this->addErrors($assetSource->getAllErrors());
91
92
                continue;
93
            }
94 2
        }
95
96 2
        if ($force) {
97 1
            foreach ($assetSources as $assetSource) {
98
                $this->getAssetSourcesService()->deleteSourceById($assetSource->id);
99 1
            }
100 1
        }
101
102 2
        return $this->getResultModel();
103
    }
104
105
    /**
106
     * Populate asset source.
107
     *
108
     * @param AssetSourceModel $assetSource
109
     * @param array            $assetSourceDefinition
110
     * @param string           $assetSourceHandle
111
     *
112
     * @return AssetSourceModel
113
     */
114 2
    private function populateAssetSource(AssetSourceModel $assetSource, array $assetSourceDefinition, $assetSourceHandle)
115
    {
116
        $defaultAssetSourceSettings = array(
117 2
            'publicURLs' => true,
118 2
        );
119
120 2
        $assetSource->setAttributes([
121 2
            'handle' => $assetSourceHandle,
122 2
            'type' => $assetSourceDefinition['type'],
123
            'name' => $assetSourceDefinition['name'],
124
            'sortOrder' => $assetSourceDefinition['sortOrder'],
125
            'settings' => array_merge($defaultAssetSourceSettings, $assetSourceDefinition['settings']),
126
        ]);
127
128
        if (array_key_exists('fieldLayout', $assetSourceDefinition)) {
129
            $fieldLayout = Craft::app()->schematic_fields->getFieldLayout($assetSourceDefinition['fieldLayout']);
130
            $assetSource->setFieldLayout($fieldLayout);
131
        }
132
133
        return $assetSource;
134
    }
135
136
    /**
137
     * Reset craft fields service cache using reflection.
138
     */
139 4
    private function resetCraftAssetSourcesServiceCache()
140
    {
141 4
        $obj = $this->getAssetSourcesService();
142 4
        $refObject = new \ReflectionObject($obj);
143 4
        if ($refObject->hasProperty('_fetchedAllSources')) {
144
            $refProperty = $refObject->getProperty('_fetchedAllSources');
145
            $refProperty->setAccessible(true);
146
            $refProperty->setValue($obj, false);
147
        }
148 4
        if ($refObject->hasProperty('_sourcesById')) {
149
            $refProperty = $refObject->getProperty('_sourcesById');
150
            $refProperty->setAccessible(true);
151
            $refProperty->setValue($obj, array());
152
        }
153 4
    }
154
}
155