Completed
Pull Request — master (#91)
by Bob Olde
03:55
created

AssetSources::export()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.9765

Importance

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