AbstractFileTask::getOptions()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Magallanes package.
5
 *
6
 * (c) Andrés Montañez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mage\Task\BuiltIn\FS;
13
14
use Mage\Task\Exception\ErrorException;
15
use Mage\Task\AbstractTask;
16
17
/**
18
 * File System Task - Abstract Base class for File Operations
19
 *
20
 * @author Andrés Montañez <[email protected]>
21
 */
22
abstract class AbstractFileTask extends AbstractTask
23
{
24
    /**
25
     * Returns the Task options
26
     *
27
     * @return array<string, string|int|null>
28
     * @throws ErrorException
29
     */
30 21
    protected function getOptions(): array
31
    {
32 21
        $mandatory = $this->getParameters();
33 21
        $defaults = array_keys($this->getDefaults());
34 21
        $missing = array_diff($mandatory, $defaults);
35
36 21
        foreach ($missing as $parameter) {
37 21
            if (!array_key_exists($parameter, $this->options)) {
38 5
                throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter));
39
            }
40
        }
41
42 16
        return $this->options;
43
    }
44
45
    /**
46
     * Returns the mandatory parameters
47
     *
48
     * @return string[]
49
     */
50
    abstract protected function getParameters(): array;
51
52
    /**
53
     * Returns a file with the placeholders replaced
54
     *
55
     * @throws ErrorException
56
     */
57 21
    protected function getFile(string $file): string
58
    {
59 21
        $mapping = [
60 21
            '%environment%' => $this->runtime->getEnvironment(),
61
        ];
62
63 21
        if ($this->runtime->getHostName() !== null) {
64 1
            $mapping['%host%'] = $this->runtime->getHostName();
65
        }
66
67 21
        if ($this->runtime->getReleaseId() !== null) {
68 1
            $mapping['%release%'] = $this->runtime->getReleaseId();
69
        }
70
71 21
        $options = $this->getOptions();
72 16
        return str_replace(
73 16
            array_keys($mapping),
74 16
            array_values($mapping),
75 16
            strval($options[$file])
76
        );
77
    }
78
}
79