Completed
Pull Request — master (#114)
by Bart
07:47
created

ImportCommand::importFromYaml()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 10
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\ConsoleCommands;
4
5
use Craft;
6
use NerdsAndCompany\Schematic\Interfaces\MappingInterface;
7
use NerdsAndCompany\Schematic\Models\Data;
8
use NerdsAndCompany\Schematic\Schematic;
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 ImportCommand extends Base
22
{
23
    public $force = false;
24
25
    /**
26
     * {@inheritdoc}
27
     *
28
     * @return array
29
     */
30
    public function options($actionID)
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()
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
        if ($this->importFromYaml($dataTypes)) {
0 ignored issues
show
Documentation introduced by
$dataTypes is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
            Schematic::info('Loaded schema from '.$this->file);
51
52
            return 0;
53
        }
54
55
        Schematic::info('There was an error loading schema from '.$this->file);
56
57
        return 1;
58
    }
59
60
    /**
61
     * Import from Yaml file.
62
     *
63
     * @param string $dataTypes The data types to import
64
     *
65
     * @return bool
66
     *
67
     * @throws Exception
68
     */
69
    private function importFromYaml($dataTypes)
70
    {
71
        $yaml = file_get_contents($this->file);
72
        $yamlOverride = null;
73
        if (file_exists($this->overrideFile)) {
74
            $yamlOverride = file_get_contents($this->overrideFile);
75
        }
76
        $dataModel = Data::fromYaml($yaml, $yamlOverride);
77
78
        foreach ($dataTypes as $dataType) {
0 ignored issues
show
Bug introduced by
The expression $dataTypes of type string is not traversable.
Loading history...
79
            $component = 'schematic_'.$dataType;
80
            if (Craft::$app->$component instanceof MappingInterface) {
81
                Schematic::info('Importing '.$dataType);
82
                Schematic::$force = $this->force;
83
                if (is_array($dataModel->$dataType)) {
84
                    $records = Schematic::getRecords($dataType);
85
                    Craft::$app->$component->import($dataModel->$dataType, $records);
86
                    if ($dataType == 'fields') {
87
                        Craft::$app->fields->updateFieldVersion();
88
                    }
89
                }
90
            } else {
91
                Schematic::error(get_class(Craft::$app->$component).' does not implement MappingInterface');
92
            }
93
        }
94
95
        return true;
96
    }
97
}
98