Completed
Pull Request — master (#114)
by Bart
01:45
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\Models\Data;
7
use NerdsAndCompany\Schematic\Schematic;
8
9
/**
10
 * Schematic Import Command.
11
 *
12
 * Sync Craft Setups.
13
 *
14
 * @author    Nerds & Company
15
 * @copyright Copyright (c) 2015-2018, Nerds & Company
16
 * @license   MIT
17
 *
18
 * @see      http://www.nerds.company
19
 */
20
class ImportController extends Base
21
{
22
    public $force = false;
23
24
    /**
25
     * {@inheritdoc}
26
     *
27
     * @return array
28
     */
29
    public function options($actionID): array
30
    {
31
        return array_merge(parent::options($actionID), ['force']);
32
    }
33
34
    /**
35
     * Imports the Craft datamodel.
36
     *
37
     * @return int
38
     */
39
    public function actionIndex(): int
40
    {
41
        if (!file_exists($this->file)) {
42
            Schematic::error('File not found: '.$this->file);
43
44
            return 1;
45
        }
46
47
        $dataTypes = $this->getDataTypes();
48
        $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...
49
        Schematic::info('Loaded schema from '.$this->file);
50
51
        return 0;
52
    }
53
54
    /**
55
     * Import from Yaml file.
56
     *
57
     * @param string $dataTypes The data types to import
58
     *
59
     * @throws Exception
60
     */
61
    private function importFromYaml($dataTypes): void
62
    {
63
        $this->disableLogging();
64
        $yaml = file_get_contents($this->file);
65
        $yamlOverride = null;
66
        if (file_exists($this->overrideFile)) {
67
            $yamlOverride = file_get_contents($this->overrideFile);
68
        }
69
        $definitions = Data::fromYaml($yaml, $yamlOverride);
70
71
        foreach ($dataTypes as $dataTypeHandle) {
0 ignored issues
show
Bug introduced by
The expression $dataTypes of type string is not traversable.
Loading history...
72
            $dataType = $this->module->getDataType($dataTypeHandle);
73
            if (null == $dataType) {
74
                continue;
75
            }
76
77
            $mapper = $dataType->getMapperHandle();
78
            if (!$this->module->checkMapper($mapper)) {
79
                continue;
80
            }
81
82
            Schematic::info('Importing '.$dataTypeHandle);
83
            Schematic::$force = $this->force;
84
            if (array_key_exists($dataTypeHandle, $definitions) && is_array($definitions[$dataTypeHandle])) {
85
                $records = $dataType->getRecords();
86
                $this->module->$mapper->import($definitions[$dataTypeHandle], $records);
87
88
                // @TODO: Don't hardcode datatype in controller
89
                if ('fields' == $dataType) {
90
                    Craft::$app->fields->updateFieldVersion();
91
                }
92
            }
93
        }
94
    }
95
}
96