Completed
Push — master ( 7f4e3a...f96930 )
by Bob Olde
04:50
created

AssetSources::getAssetSourceDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
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 3
        }
45
46 3
        return $assetSourceDefinitions;
47
    }
48
49
    /**
50
     * @param AssetSourceModel $assetSource
51
     *
52
     * @return array
53
     */
54 2
    private function getAssetSourceDefinition(AssetSourceModel $assetSource)
55
    {
56 2
        $fieldLayout = Craft::app()->fields->getLayoutById($assetSource->fieldLayoutId);
57
58
        return [
59 2
            'type' => $assetSource->type,
60 2
            'name' => $assetSource->name,
61 2
            'sortOrder' => $assetSource->sortOrder,
62 2
            'settings' => $assetSource->settings,
63 2
            'fieldLayout' => Craft::app()->schematic_fields->getFieldLayoutDefinition($fieldLayout),
64 2
        ];
65
    }
66
67
    /**
68
     * Import asset source definitions.
69
     *
70
     * @param array $assetSourceDefinitions
71
     * @param bool  $force
72
     *
73
     * @return Result
74
     */
75 4
    public function import(array $assetSourceDefinitions, $force = false)
76
    {
77 4
        Craft::log(Craft::t('Importing Asset Sources'));
78
79 4
        $this->resetCraftAssetSourcesServiceCache();
80 4
        $assetSources = $this->getAssetSourcesService()->getAllSources('handle');
81
82 4
        foreach ($assetSourceDefinitions as $assetSourceHandle => $assetSourceDefinition) {
83 2
            $assetSource = array_key_exists($assetSourceHandle, $assetSources)
84 2
                ? $assetSources[$assetSourceHandle]
85 2
                : new AssetSourceModel();
86
87 2
            unset($assetSources[$assetSourceHandle]);
88
89 2
            $this->populateAssetSource($assetSource, $assetSourceDefinition, $assetSourceHandle);
90
91 2
            if (!$this->getAssetSourcesService()->saveSource($assetSource)) { // Save assetsource via craft
92 2
                $this->addErrors($assetSource->getAllErrors());
93
94 2
                continue;
95
            }
96 4
        }
97
98 4
        if ($force) {
99 2
            foreach ($assetSources as $assetSource) {
100
                $this->getAssetSourcesService()->deleteSourceById($assetSource->id);
101 2
            }
102 2
        }
103
104 4
        return $this->getResultModel();
105
    }
106
107
    /**
108
     * Populate asset source.
109
     *
110
     * @param AssetSourceModel $assetSource
111
     * @param array            $assetSourceDefinition
112
     * @param string           $assetSourceHandle
113
     *
114
     * @return AssetSourceModel
115
     */
116 2
    private function populateAssetSource(AssetSourceModel $assetSource, array $assetSourceDefinition, $assetSourceHandle)
117
    {
118
        $defaultAssetSourceSettings = array(
119 2
            'publicURLs' => true,
120 2
        );
121
122 2
        $assetSource->setAttributes([
123 2
            'handle' => $assetSourceHandle,
124 2
            'type' => $assetSourceDefinition['type'],
125 2
            'name' => $assetSourceDefinition['name'],
126 2
            'sortOrder' => $assetSourceDefinition['sortOrder'],
127 2
            'settings' => array_merge($defaultAssetSourceSettings, $assetSourceDefinition['settings']),
128 2
        ]);
129
130 2
        if (array_key_exists('fieldLayout', $assetSourceDefinition)) {
131 2
            $fieldLayout = Craft::app()->schematic_fields->getFieldLayout($assetSourceDefinition['fieldLayout']);
132 2
            $assetSource->setFieldLayout($fieldLayout);
133 2
        }
134
135 2
        return $assetSource;
136
    }
137
138
    /**
139
     * Reset craft fields service cache using reflection.
140
     */
141 4
    private function resetCraftAssetSourcesServiceCache()
142
    {
143 4
        $obj = $this->getAssetSourcesService();
144 4
        $refObject = new \ReflectionObject($obj);
145 4
        if ($refObject->hasProperty('_fetchedAllSources')) {
146
            $refProperty = $refObject->getProperty('_fetchedAllSources');
147
            $refProperty->setAccessible(true);
148
            $refProperty->setValue($obj, false);
149
        }
150 4
        if ($refObject->hasProperty('_sourcesById')) {
151
            $refProperty = $refObject->getProperty('_sourcesById');
152
            $refProperty->setAccessible(true);
153
            $refProperty->setValue($obj, array());
154
        }
155 4
    }
156
}
157