Completed
Pull Request — master (#114)
by Bart
02:13
created

ImportController::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Controllers;
4
5
use Craft;
6
use NerdsAndCompany\Schematic\Interfaces\DataTypeInterface;
7
use NerdsAndCompany\Schematic\Interfaces\MapperInterface;
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 ImportController 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
46
            return 1;
47
        }
48
49
        $dataTypes = $this->getDataTypes();
50
        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...
51
            Schematic::info('Loaded schema from '.$this->file);
52
53
            return 0;
54
        }
55
56
        Schematic::info('There was an error loading schema from '.$this->file);
57
58
        return 1;
59
    }
60
61
    /**
62
     * Import from Yaml file.
63
     *
64
     * @param string $dataTypes The data types to import
65
     *
66
     * @return bool
67
     *
68
     * @throws Exception
69
     */
70
    private function importFromYaml($dataTypes)
71
    {
72
        $this->disableLogging();
73
        $yaml = file_get_contents($this->file);
74
        $yamlOverride = null;
75
        if (file_exists($this->overrideFile)) {
76
            $yamlOverride = file_get_contents($this->overrideFile);
77
        }
78
        $definitions = Data::fromYaml($yaml, $yamlOverride);
79
80
        foreach ($dataTypes as $dataTypeHandle) {
0 ignored issues
show
Bug introduced by
The expression $dataTypes of type string is not traversable.
Loading history...
81
            $dataTypeClass = $this->module->dataTypes[$dataTypeHandle];
82
            $dataType = new $dataTypeClass();
83
            if (!$dataType instanceof DataTypeInterface) {
84
                Schematic::error($dataTypeClass.' does not implement DataTypeInterface');
85
                continue;
86
            }
87
88
            $mapper = $dataType->getMapperHandle();
89
            if (!$this->module->$mapper instanceof MapperInterface) {
90
                Schematic::error(get_class($this->module->$mapper).' does not implement MapperInterface');
91
                continue;
92
            }
93
94
            Schematic::info('Importing '.$dataTypeHandle);
95
            Schematic::$force = $this->force;
96
            if (array_key_exists($dataTypeHandle, $definitions) && is_array($definitions[$dataTypeHandle])) {
97
                $records = $dataType->getRecords();
98
                $this->module->$mapper->import($definitions[$dataTypeHandle], $records);
99
100
                // @TODO: Don't hardcode datatype in controller
101
                if ('fields' == $dataType) {
102
                    Craft::$app->fields->updateFieldVersion();
103
                }
104
            }
105
        }
106
107
        return true;
108
    }
109
}
110