Completed
Pull Request — master (#167)
by
unknown
02:25
created

Base::toSafeFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace NerdsAndCompany\Schematic\Controllers;
4
5
use Craft;
6
use yii\console\Controller;
7
use NerdsAndCompany\Schematic\Schematic;
8
9
/**
10
 * Schematic Base Controller.
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 Base extends Controller
21
{
22
    public $file = 'config/schema.yml';
23
    public $path = 'config/schema/';
24
    public $overrideFile = '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', 'overrideFile', '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
        //Import fields and usergroups again after all sources have been imported
58
        if (array_search('fields', $dataTypes) && count($dataTypes) > 1) {
59
            $dataTypes[] = 'fields';
60
            $dataTypes[] = 'userGroups';
61
        }
62
63
        return $dataTypes;
64
    }
65
66
    /**
67
     * Apply given includes.
68
     *
69
     * @param array $dataTypes
70
     *
71
     * @return array
72
     */
73
    protected function applyIncludes($dataTypes): array
74
    {
75
        $inclusions = explode(',', $this->include);
76
        // Find any invalid data to include.
77
        $invalidIncludes = array_diff($inclusions, $dataTypes);
78
        if (count($invalidIncludes) > 0) {
79
            $errorMessage = 'WARNING: Invalid include(s)';
80
            $errorMessage .= ': '.implode(', ', $invalidIncludes).'.'.PHP_EOL;
81
            $errorMessage .= ' Valid inclusions are '.implode(', ', $dataTypes);
82
83
            // Output an error message outlining what invalid exclusions were specified.
84
            Schematic::warning($errorMessage);
85
        }
86
        // Remove any explicitly included data types from the list of data types to export.
87
        return array_intersect($dataTypes, $inclusions);
88
    }
89
90
    /**
91
     * Apply given excludes.
92
     *
93
     * @param array $dataTypes
94
     *
95
     * @return array
96
     */
97
    protected function applyExcludes(array $dataTypes): array
98
    {
99
        $exclusions = explode(',', $this->exclude);
100
        // Find any invalid data to exclude.
101
        $invalidExcludes = array_diff($exclusions, $dataTypes);
102
        if (count($invalidExcludes) > 0) {
103
            $errorMessage = 'WARNING: Invalid exlude(s)';
104
            $errorMessage .= ': '.implode(', ', $invalidExcludes).'.'.PHP_EOL;
105
            $errorMessage .= ' Valid exclusions are '.implode(', ', $dataTypes);
106
107
            // Output an error message outlining what invalid exclusions were specified.
108
            Schematic::warning($errorMessage);
109
        }
110
        // Remove any explicitly excluded data types from the list of data types to export.
111
        return array_diff($dataTypes, $exclusions);
112
    }
113
114
    /**
115
     * Disable normal logging (to stdout) while running console commands.
116
     *
117
     * @TODO: Find a less hacky way to solve this
118
     */
119
    protected function disableLogging()
120
    {
121
        if (Craft::$app->log) {
122
            Craft::$app->log->targets = [];
123
        }
124
    }
125
126
    /**
127
     * Convert a filename to one safe to use.
128
     *
129
     * @param $fileName
130
     * @return mixed
131
     */
132
    protected function toSafeFileName($fileName) {
133
        // Remove all slashes and backslashes in the recordName to avoid file sturcture problems.
134
        $fileName = str_replace('\\', ':', $fileName);
135
        $fileName = str_replace('/', '::', $fileName);
136
137
        return $fileName;
138
    }
139
140
    /**
141
     * Convert a safe filename back to it's original form.
142
     *
143
     * @param $fileName
144
     * @return mixed
145
     */
146
    protected function fromSafeFileName($fileName) {
147
        // Replace the placeholders back to slashes and backslashes.
148
        $fileName = str_replace(':', '\\', $fileName);
149
        $fileName = str_replace('::', '/', $fileName);
150
151
        return $fileName;
152
    }
153
}
154