Completed
Push — master ( 33ec04...ed9cfc )
by Bart
11s
created

src/Controllers/ImportController.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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);
0 ignored issues
show
$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
    /**
56
     * Import from Yaml file.
57
     *
58
     * @param string $dataTypes The data types to import
59
     *
60
     * @throws Exception
61
     */
62
    private function importFromYaml($dataTypes): void
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) {
0 ignored issues
show
The expression $dataTypes of type string is not traversable.
Loading history...
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
90
                    // @TODO: Don't hardcode datatype in controller
91
                    if ('fields' == $dataTypeHandle) {
92
                        Craft::$app->fields->updateFieldVersion();
93
                    }
94
                } catch (WrongEditionException $e) {
95
                    Schematic::error('Craft Pro is required for datatype '.$dataTypeHandle);
96
                }
97
            }
98
        }
99
    }
100
}
101