Completed
Pull Request — master (#114)
by Bart
01:46
created

Base::applyExcludes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Controllers;
4
5
use Craft;
6
use yii\base\Behavior;
7
use yii\console\Controller;
8
use NerdsAndCompany\Schematic\Schematic;
9
10
/**
11
 * Schematic FieldLayout Behavior.
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 Base extends Controller
22
{
23
    public $file = 'config/schema.yml';
24
    public $overrideFile = 'craft/config/override.yml';
25
    public $exclude;
26
    public $include;
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @return array
32
     */
33
    public function options($actionID): array
34
    {
35
        return ['file', 'override_file', 'include', 'exclude'];
36
    }
37
38
    /**
39
     * Get the datatypes to import and/or export.
40
     *
41
     * @return array
42
     */
43
    protected function getDataTypes(): array
44
    {
45
        $dataTypes = array_keys($this->module->dataTypes);
46
47
        // If include is specified.
48
        if (null !== $this->include) {
49
            $dataTypes = $this->applyIncludes($dataTypes);
50
        }
51
52
        // If there are exclusions.
53
        if (null !== $this->exclude) {
54
            $dataTypes = $this->applyExcludes($dataTypes);
55
        }
56
57
        return $dataTypes;
58
    }
59
60
    /**
61
     * Apply given includes.
62
     *
63
     * @param array $dataTypes
64
     *
65
     * @return array
66
     */
67
    protected function applyIncludes($dataTypes): array
68
    {
69
        $inclusions = explode(',', $this->include);
70
        // Find any invalid data to include.
71
        $invalidIncludes = array_diff($inclusions, $dataTypes);
72
        if (count($invalidIncludes) > 0) {
73
            $errorMessage = 'WARNING: Invalid include(s)';
74
            $errorMessage .= ': '.implode(', ', $invalidIncludes).'.'.PHP_EOL;
75
            $errorMessage .= ' Valid inclusions are '.implode(', ', $dataTypes);
76
77
            // Output an error message outlining what invalid exclusions were specified.
78
            Schematic::warning($errorMessage);
79
        }
80
        // Remove any explicitly included data types from the list of data types to export.
81
        return array_intersect($dataTypes, $inclusions);
82
    }
83
84
    /**
85
     * Apply given excludes.
86
     *
87
     * @param array $dataTypes
88
     *
89
     * @return array
90
     */
91
    protected function applyExcludes(array $dataTypes): array
92
    {
93
        $exclusions = explode(',', $this->exclude);
94
        // Find any invalid data to exclude.
95
        $invalidExcludes = array_diff($exclusions, $dataTypes);
96
        if (count($invalidExcludes) > 0) {
97
            $errorMessage = 'WARNING: Invalid exlude(s)';
98
            $errorMessage .= ': '.implode(', ', $invalidExcludes).'.'.PHP_EOL;
99
            $errorMessage .= ' Valid exclusions are '.implode(', ', $dataTypes);
100
101
            // Output an error message outlining what invalid exclusions were specified.
102
            Schematic::warning($errorMessage);
103
        }
104
        // Remove any explicitly excluded data types from the list of data types to export.
105
        return array_diff($dataTypes, $exclusions);
106
    }
107
108
    /**
109
     * Disable normal logging (to stdout) while running console commands.
110
     *
111
     * @TODO: Find a less hacky way to solve this
112
     */
113
    protected function disableLogging(): void
114
    {
115
        if (Craft::$app->log) {
116
            Craft::$app->log->targets = [];
117
        }
118
    }
119
}
120