Completed
Pull Request — master (#167)
by
unknown
31:42
created

ExportController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 38
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Controllers;
4
5
use Craft;
6
use craft\helpers\FileHelper;
7
use NerdsAndCompany\Schematic\Models\Data;
8
use NerdsAndCompany\Schematic\Schematic;
9
10
/**
11
 * Schematic Export Controller.
12
 *
13
 * Sync Craft Setups.
14
 *
15
 * @author    Nerds & Company
16
 * @copyright Copyright (c) 2015-2018, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @see      http://www.nerds.company
20
 */
21
class ExportController extends Base
22
{
23
    /**
24
     * Exports the Craft datamodel.
25
     *
26
     * @return int
27
     * @throws \yii\base\ErrorException
28
     */
29
    public function actionIndex(): int
30
    {
31
        $this->disableLogging();
32
        $result = [];
33
        foreach ($this->getDataTypes() as $dataTypeHandle) {
34
            $dataType = $this->module->getDataType($dataTypeHandle);
35
            if (null == $dataType) {
36
                continue;
37
            }
38
39
            $mapper = $dataType->getMapperHandle();
40
            if (!$this->module->checkMapper($mapper)) {
41
                continue;
42
            }
43
44
            $records = $dataType->getRecords();
45
            $configurations[$dataTypeHandle] = $this->module->$mapper->export($records);
46
        }
47
48
        // Parse data in the overrideFile if available.
49
        $overrideData = Data::parseYamlFile($this->overrideFile);
50
51
        // Create export directory if it doesn't exist.
52
        if (!file_exists($this->path)) {
53
            mkdir($this->path, 2775, true);
54
        }
55
56
<<<<<<< Updated upstream
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_SL
Loading history...
57
        // Create export directory if it doesn't exist.
58
        if (!file_exists($this->path)) {
59
            mkdir($this->path, 2775, true);
60
        }
61
62
=======
63
>>>>>>> Stashed changes
64
        // Export the configuration to multiple yaml files.
65
        foreach ($configurations as $dataTypeHandle => $configuration) {
66
            Schematic::info('Exporting '.$dataTypeHandle);
67
            foreach ($configuration as $recordName => $records) {
68
<<<<<<< Updated upstream
69
=======
70
                // Check if there is data in the override file for the current record.
71
                if (isset($overrideData[$dataTypeHandle][$recordName])) {
72
                    $records = array_replace_recursive($records, $overrideData[$dataTypeHandle][$recordName]);
73
                }
74
75
                // Export records to file.
76
>>>>>>> Stashed changes
77
                $fileName = $this->toSafeFileName($dataTypeHandle . '.' . $recordName . '.yml');
78
                FileHelper::writeToFile($this->path . $fileName, Data::toYaml($records));
79
                Schematic::info('Exported ' . $recordName . ' to ' . $fileName);
80
            }
81
        }
82
83
        return 0;
84
    }
85
}
86