Completed
Pull Request — master (#143)
by Bart
08:20 queued 05:20
created

ImportController::importFromYaml()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 31
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 23
nc 14
nop 1
crap 72
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Controllers;
4
5
use Craft;
6
use NerdsAndCompany\Schematic\Models\Data;
7
use NerdsAndCompany\Schematic\Schematic;
8
use craft\errors\WrongEditionException;
9
10
/**
11
 * Schematic Import Command.
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 ImportController extends Base
22
{
23
    public $force = false;
24
25
    /**
26
     * {@inheritdoc}
27
     *
28
     * @return array
29
     */
30
    public function options($actionID): array
31
    {
32
        return array_merge(parent::options($actionID), ['force']);
33
    }
34
35
    /**
36
     * Imports the Craft datamodel.
37
     *
38
     * @return int
39
     */
40
    public function actionIndex(): int
41
    {
42
        if (!file_exists($this->file)) {
43
            Schematic::error('File not found: '.$this->file);
44
45
            return 1;
46
        }
47
48
        $dataTypes = $this->getDataTypes();
49
        $this->importFromYaml($dataTypes);
50
        Schematic::info('Loaded schema from '.$this->file);
51
52
        return 0;
53
    }
54
55
    /**
56
     * Import from Yaml file.
57
     *
58
     * @param array $dataTypes The data types to import
59
     *
60
     * @throws Exception
61
     */
62
    private function importFromYaml(array $dataTypes)
63
    {
64
        $this->disableLogging();
65
        $yaml = file_get_contents($this->file);
66
        $yamlOverride = null;
67
        if (file_exists($this->overrideFile)) {
68
            $yamlOverride = file_get_contents($this->overrideFile);
69
        }
70
        $definitions = Data::fromYaml($yaml, $yamlOverride);
71
72
        foreach ($dataTypes as $dataTypeHandle) {
73
            $dataType = $this->module->getDataType($dataTypeHandle);
74
            if (null == $dataType) {
75
                continue;
76
            }
77
78
            $mapper = $dataType->getMapperHandle();
79
            if (!$this->module->checkMapper($mapper)) {
80
                continue;
81
            }
82
83
            Schematic::info('Importing '.$dataTypeHandle);
84
            Schematic::$force = $this->force;
85
            if (array_key_exists($dataTypeHandle, $definitions) && is_array($definitions[$dataTypeHandle])) {
86
                $records = $dataType->getRecords();
87
                try {
88
                    $this->module->$mapper->import($definitions[$dataTypeHandle], $records);
89
                    $dataType->afterImport();
90
                } catch (WrongEditionException $e) {
91
                    Schematic::error('Craft Pro is required for datatype '.$dataTypeHandle);
92
                }
93
            }
94
        }
95
    }
96
}
97