Completed
Pull Request — master (#114)
by Bart
09:14
created

ImportCommand::importFromYaml()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 14
nc 6
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\ConsoleCommands;
4
5
use Craft;
6
use craft\helpers\FileHelper;
7
use NerdsAndCompany\Schematic\Interfaces\MappingInterface;
8
use NerdsAndCompany\Schematic\Models\Data;
9
use NerdsAndCompany\Schematic\Schematic;
10
11
/**
12
 * Schematic Import Command.
13
 *
14
 * Sync Craft Setups.
15
 *
16
 * @author    Nerds & Company
17
 * @copyright Copyright (c) 2015-2018, Nerds & Company
18
 * @license   MIT
19
 *
20
 * @see      http://www.nerds.company
21
 */
22
class ImportCommand extends Base
23
{
24
    public $force = false;
25
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @return array
30
     */
31
    public function options($actionID)
32
    {
33
        return array_merge(parent::options($actionID), ['force']);
34
    }
35
36
    /**
37
     * Imports the Craft datamodel.
38
     *
39
     * @return int
40
     */
41
    public function actionIndex()
42
    {
43
        if (!file_exists($this->file)) {
44
            Schematic::error('File not found: '.$this->file);
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
            return 0;
52
        }
53
54
        Schematic::info('There was an error loading schema from '. $this->file);
55
        return 1;
56
    }
57
58
    /**
59
     * Import from Yaml file.
60
     *
61
     * @param string $dataTypes The data types to import
62
     *
63
     * @return boolean
64
     * @throws Exception
65
     */
66
    private function importFromYaml($dataTypes)
67
    {
68
        $yaml = file_get_contents($this->file);
69
        $yamlOverride = null;
70
        if (file_exists($this->overrideFile)) {
71
            $yamlOverride = file_get_contents($this->overrideFile);
72
        }
73
        $dataModel = Data::fromYaml($yaml, $yamlOverride);
74
75
        foreach (array_keys($dataTypes) as $dataType) {
76
            $component = 'schematic_'.$dataType;
77
            if (Craft::$app->$component instanceof MappingInterface) {
78
                Schematic::info('Importing '.$dataType);
79
                Craft::$app->$component->import($dataModel->$dataType, $this->force);
80
            } else {
81
                Schematic::error(get_class(Craft::$app->$component).' does not implement MappingInterface');
82
            }
83
        }
84
85
        return true;
86
    }
87
}
88