Completed
Push — master ( 27039d...db9c49 )
by Bart
13s
created

ImportCommand::actionIndex()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 47
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 22
nc 12
nop 4
1
<?php
2
3
namespace NerdsAndCompany\Schematic\ConsoleCommands;
4
5
use Craft\Craft;
6
use Craft\BaseCommand as Base;
7
use Craft\IOHelper;
8
use NerdsAndCompany\Schematic\Services\Schematic;
9
10
/**
11
 * Schematic Import Command.
12
 *
13
 * Sync Craft Setups.
14
 *
15
 * @author    Nerds & Company
16
 * @copyright Copyright (c) 2015-2017, Nerds & Company
17
 * @license   MIT
18
 *
19
 * @see      http://www.nerds.company
20
 */
21
class ImportCommand extends Base
22
{
23
    /**
24
     * Imports the Craft datamodel.
25
     *
26
     * @param string $file          yml file containing the schema definition
27
     * @param string $override_file yml file containing the override values
28
     * @param bool   $force         if set to true items not in the import will be deleted
29
     * @param array  $exclude       Data to not import
30
     *
31
     * @return int
32
     */
33
    public function actionIndex($file = 'craft/config/schema.yml', $override_file = 'craft/config/override.yml', $force = false, array $exclude = null)
34
    {
35
        if (!IOHelper::fileExists($file)) {
36
            $this->usageError(Craft::t('File not found.'));
37
        }
38
39
        $dataTypes = Schematic::getExportableDataTypes();
40
41
        // If there are data exclusions.
42
        if ($exclude !== null) {
43
            // Find any invalid data to exclude.
44
            $invalidExcludes = array_diff($exclude, $dataTypes);
45
46
            // If any invalid exclusions were specified.
47
            if (count($invalidExcludes) > 0) {
48
                $errorMessage = 'Invalid exlude';
49
50
                if (count($invalidExcludes) > 1) {
51
                    $errorMessage .= 's';
52
                }
53
54
                $errorMessage .= ': '.implode(', ', $invalidExcludes).'.';
55
                $errorMessage .= ' Valid exclusions are '.implode(', ', $dataTypes);
56
57
                // Output an error message outlining what invalid exclusions were specified.
58
                echo "\n".$errorMessage."\n\n";
59
60
                return 1;
61
            }
62
63
            // Remove any explicitly excluded data types from the list of data types to export.
64
            $dataTypes = array_diff($dataTypes, $exclude);
65
        }
66
67
        $result = Craft::app()->schematic->importFromYaml($file, $override_file, $force, $dataTypes);
68
69
        if (!$result->hasErrors()) {
70
            Craft::log(Craft::t('Loaded schema from {file}', ['file' => $file]));
71
72
            return 0;
73
        }
74
75
        Craft::log(Craft::t('There was an error loading schema from {file}', ['file' => $file]));
76
        print_r($result->getErrors());
77
78
        return 1;
79
    }
80
}
81