Completed
Pull Request — master (#114)
by Bart
16:27 queued 06:28
created

ExportCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 119
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 4 1
A actionIndex() 0 19 3
A exportToYaml() 0 20 4
A applyIncludes() 0 16 2
A applyExcludes() 0 16 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\ConsoleCommands;
4
5
use Craft;
6
use craft\helpers\FileHelper;
7
use NerdsAndCompany\Schematic\Schematic;
8
use NerdsAndCompany\Schematic\Interfaces\MappingInterface;
9
use Symfony\Component\Yaml\Yaml;
10
use yii\console\Controller as Base;
11
12
/**
13
 * Schematic Export Command.
14
 *
15
 * Sync Craft Setups.
16
 *
17
 * @author    Nerds & Company
18
 * @copyright Copyright (c) 2015-2018, Nerds & Company
19
 * @license   MIT
20
 *
21
 * @see      http://www.nerds.company
22
 */
23
class ExportCommand extends Base
24
{
25
    public $file = 'config/schema.yml';
26
    public $exclude;
27
    public $include;
28
29
    /**
30
     * {@inheritdoc}
31
     *
32
     * @return array
33
     */
34
    public function options($actionID)
35
    {
36
        return ['file', 'include', 'exclude'];
37
    }
38
39
    /**
40
     * Exports the Craft datamodel.
41
     *
42
     * @param string $file    file to write the schema to
0 ignored issues
show
Bug introduced by
There is no parameter named $file. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
43
     * @param array  $exclude Data to not export
0 ignored issues
show
Bug introduced by
There is no parameter named $exclude. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
44
     *
45
     * @return int
46
     */
47
    public function actionIndex()
48
    {
49
        $dataTypes = Schematic::DATA_TYPES;
50
51
        // If include is specified.
52
        if ($this->include !== null) {
53
            $dataTypes = $this->applyIncludes($dataTypes);
54
        }
55
56
        // If there are exclusions.
57
        if ($this->exclude !== null) {
58
            $dataTypes = $this->applyExcludes($dataTypes);
59
        }
60
61
        $this->exportToYaml($this->file, $dataTypes);
62
        Craft::info('Exported schema to '.$this->file, 'schematic');
63
64
        return 0;
65
    }
66
67
    /**
68
     * Export to Yaml file.
69
     *
70
     * @param string $file
71
     * @param bool   $autoCreate
0 ignored issues
show
Bug introduced by
There is no parameter named $autoCreate. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
72
     *
73
     * @return Result
74
     */
75
    public function exportToYaml($file, $dataTypes)
76
    {
77
        $result = [];
78
        foreach (array_keys($dataTypes) as $dataType) {
79
            $component = 'schematic_'.$dataType;
80
            if (Craft::$app->$component instanceof MappingInterface) {
81
                Craft::info('Exporting '.$dataType, 'schematic');
82
                $result[$dataType] = Craft::$app->$component->export();
83
            } else {
84
                Craft::error(get_class(Craft::$app->$component).' does not implement MappingInterface', 'schematic');
85
            }
86
        }
87
88
        $yaml = Yaml::dump($result, 10);
89
        if (!FileHelper::writeToFile($file, $yaml)) {
90
            Craft::error('error', "Failed to write contents to \"$file\"", 'schematic');
0 ignored issues
show
Unused Code introduced by
The call to Craft::error() has too many arguments starting with 'schematic'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
91
        }
92
93
        return true;
94
    }
95
96
    /**
97
     * Apply given includes
98
     *
99
     * @param  array $dataTypes
100
     * @return array
101
     */
102
    private function applyIncludes($dataTypes)
103
    {
104
        $inclusions = explode(',', $this->include);
105
        // Find any invalid data to include.
106
        $invalidIncludes = array_diff($inclusions, $dataTypes);
107
        if (count($invalidIncludes) > 0) {
108
            $errorMessage = 'WARNING: Invalid include(s)';
109
            $errorMessage .= ': '.implode(', ', $invalidIncludes).'.'.PHP_EOL;
110
            $errorMessage .= ' Valid inclusions are '.implode(', ', $dataTypes);
111
112
            // Output an error message outlining what invalid exclusions were specified.
113
            Craft::warning($errorMessage, 'schematic');
114
        }
115
        // Remove any explicitly included data types from the list of data types to export.
116
        return array_intersect($dataTypes, $inclusions);
117
    }
118
119
    /**
120
     * Apply given excludes
121
     *
122
     * @param  array $dataTypes
123
     * @return array
124
     */
125
    private function applyExcludes(array $dataTypes)
126
    {
127
        $exclusions = explode(',', $this->exclude);
128
        // Find any invalid data to exclude.
129
        $invalidExcludes = array_diff($exclusions, $dataTypes);
130
        if (count($invalidExcludes) > 0) {
131
            $errorMessage = 'WARNING: Invalid exlude(s)';
132
            $errorMessage .= ': '.implode(', ', $invalidExcludes).'.'.PHP_EOL;
133
            $errorMessage .= ' Valid exclusions are '.implode(', ', $dataTypes);
134
135
            // Output an error message outlining what invalid exclusions were specified.
136
            Craft::warning($errorMessage, 'schematic');
137
        }
138
        // Remove any explicitly excluded data types from the list of data types to export.
139
        return array_diff($dataTypes, $exclusions);
140
    }
141
}
142