Completed
Push — master ( a3a102...0d148f )
by Bart
02:51 queued 11s
created

ExportController::actionIndex()   C

Complexity

Conditions 11
Paths 48

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 0
cts 43
cp 0
rs 6.7478
c 0
b 0
f 0
cc 11
nc 48
nop 0
crap 132

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
33
        $configurations = [];
34
        foreach ($this->getDataTypes() as $dataTypeHandle) {
35
            $dataType = $this->module->getDataType($dataTypeHandle);
36
            if (null == $dataType) {
37
                continue;
38
            }
39
40
            $mapper = $dataType->getMapperHandle();
41
            if (!$this->module->checkMapper($mapper)) {
42
                continue;
43
            }
44
45
            $records = $dataType->getRecords();
46
            $configurations[$dataTypeHandle] = $this->module->$mapper->export($records);
47
        }
48
49
        // Load override file.
50
        $overrideData = [];
51
        if (file_exists($this->overrideFile)) {
52
            // Parse data in the overrideFile if available.
53
            $overrideData = Data::parseYamlFile($this->overrideFile);
54
        }
55
56
        // Export the configuration to a single file.
57
        if ($this->getStorageType() === self::SINGLE_FILE) {
58
            $configurations = array_replace_recursive($configurations, $overrideData);
59
            FileHelper::writeToFile($this->file, Data::toYaml($configurations));
60
            Schematic::info('Exported schema to '.$this->file);
61
        }
62
63
        // Export the configuration to multiple yaml files.
64
        if ($this->getStorageType() === self::MULTIPLE_FILES) {
65
            // Create export directory if it doesn't exist.
66
            if (!file_exists($this->path)) {
67
                mkdir($this->path, 2775, true);
68
            }
69
70
            foreach ($configurations as $dataTypeHandle => $configuration) {
71
                Schematic::info('Exporting '.$dataTypeHandle);
72
                foreach ($configuration as $recordName => $records) {
73
                    // Check if there is data in the override file for the current record.
74
                    if (isset($overrideData[$dataTypeHandle][$recordName])) {
75
                        $records = array_replace_recursive($records, $overrideData[$dataTypeHandle][$recordName]);
76
                    }
77
78
                    // Export records to file.
79
                    $fileName = $this->toSafeFileName($dataTypeHandle.'.'.$recordName.'.yml');
80
                    FileHelper::writeToFile($this->path.$fileName, Data::toYaml($records));
81
                    Schematic::info('Exported '.$recordName.' to '.$fileName);
82
                }
83
            }
84
        }
85
86
        return 0;
87
    }
88
}
89