ParameterFactory::getDefaultValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Startwind\Forrest\Command\Parameters;
4
5
use Startwind\Forrest\Command\Parameters\Validation\Constraint\ConstraintFactory;
6
use Startwind\Forrest\Enrichment\EnrichFunction\String\FunctionComposite;
7
8
class ParameterFactory
9
{
10
    private const TYPE_MIXED = 'forrest_mixed';
11
    private const TYPE_FILENAME = 'forrest_filename';
12
    private const TYPE_PASSWORD = 'forrest_password';
13
    private const FIELD_TYPE = 'type';
14
    private const FIELD_CONSTRAINTS = 'constraints';
15
16
    /**
17
     * Create a Parameter configuration object from the given config array.
18
     */
19
    public static function create(array $config): Parameter
20
    {
21
        if (array_key_exists(self::FIELD_TYPE, $config)) {
22
            $type = $config[self::FIELD_TYPE];
23
        } else {
24
            $type = self::TYPE_MIXED;
25
        }
26
27
        switch ($type) {
28
            case self::TYPE_FILENAME:
29
                $parameter = self::createFileParameter($config);
30
                break;
31
            case self::TYPE_PASSWORD:
32
                $parameter = new PasswordParameter();
33
                break;
34
            default:
35
                $parameter = self::createMixedParameter($config);
36
        }
37
38
        self::enrichParameters($parameter, $config);
39
40
        return $parameter;
41
    }
42
43
    private static function enrichParameters(Parameter $parameter, array $config): void
44
    {
45
        $parameter->setRawStructure($config);
46
47
        if (array_key_exists('name', $config)) {
48
            $parameter->setName($config['name']);
49
        }
50
51
        if (array_key_exists('forceDefault', $config)) {
52
            $parameter->setForceDefault($config['forceDefault']);
53
        }
54
55
        if (array_key_exists('description', $config)) {
56
            $parameter->setDescription($config['description']);
57
        }
58
59
        if (array_key_exists('default', $config)) {
60
            if (!is_string($config['default']) && !is_int($config['default'])) {
61
                throw new \RuntimeException('The default value must be a string or an integer (name: ' . $config['name'] . '). ' . ucfirst(gettype($config['default'])) . ' with value ' . json_encode($config['default']) . ' given.');
62
            }
63
            $defaultValue = self::getDefaultValue($config['default']);
64
            if ($defaultValue !== '') {
65
                $parameter->setDefaultValue($defaultValue);
66
            }
67
        }
68
69
        if (array_key_exists(self::FIELD_CONSTRAINTS, $config)) {
70
            $constraints = self::getConstraints($config[self::FIELD_CONSTRAINTS]);
71
            $parameter->setConstraints($constraints);
72
        }
73
74
        if (!array_key_exists('enum-allow-custom', $config)) {
75
            $enumAllowCustom = false;
76
        } else {
77
            $enumAllowCustom = $config['enum-allow-custom'];
78
        }
79
80
        if (array_key_exists('enum', $config)) {
81
            $parameter->setValues($config['enum'], $enumAllowCustom);
82
        }
83
84
        if (array_key_exists('prefix', $config)) {
85
            $parameter->setPrefix($config['prefix']);
86
        }
87
88
        if (array_key_exists('suffix', $config)) {
89
            $parameter->setSuffix($config['suffix']);
90
        }
91
92
        if (array_key_exists('optional', $config)) {
93
            $parameter->setOptional((bool)$config['optional']);
94
        }
95
    }
96
97
    private static function getConstraints(array $constraintArray): array
98
    {
99
        $constraints = [];
100
101
        foreach ($constraintArray as $constraint) {
102
            try {
103
                if (is_null($constraint)) {
104
                    throw new \RuntimeException('The given constraint must not be null.');
105
                }
106
                $constraints[] = ConstraintFactory::getConstraint($constraint);
107
            } catch (\Exception $exception) {
108
                // @todo log error
109
            }
110
        }
111
112
        return $constraints;
113
    }
114
115
    /**
116
     * Get the default value. This also handles ENV variables
117
     */
118
    private static function getDefaultValue($configElement): string
119
    {
120
        $functionComposite = new FunctionComposite();
121
        return $functionComposite->applyFunction($configElement);
122
    }
123
124
    private static function createMixedParameter(array $config): Parameter
125
    {
126
        if (empty($config)) {
127
            return new UndefinedParameter();
128
        } else {
129
            return new Parameter();
130
        }
131
    }
132
133
    private static function createFileParameter($config): FileParameter
134
    {
135
        $fileParameter = new FileParameter();
136
137
        if (array_key_exists('file-formats', $config)) {
138
            $fileParameter->setFileFormats($config['file-formats']);
139
        }
140
141
        return $fileParameter;
142
    }
143
}
144