Completed
Push — master ( 80ba2b...8bd5d2 )
by Craig
14:31
created

YamlDumper::validateName()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 4
nc 3
nop 2
dl 0
loc 7
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreBundle;
15
16
use InvalidArgumentException;
17
use Symfony\Component\Filesystem\Filesystem;
18
use Symfony\Component\Yaml\Yaml;
19
20
/**
21
 * @deprecated
22
 * @internal
23
 */
24
class YamlDumper
25
{
26
    /**
27
     * @var Filesystem
28
     */
29
    protected $fs;
30
31
    /**
32
     * @var string
33
     */
34
    protected $fullPath;
35
36
    public function __construct(string $configDir, string $filePath = 'services_custom.yaml')
37
    {
38
        $this->fullPath = $configDir . DIRECTORY_SEPARATOR . $filePath;
39
        $this->fs = new Filesystem();
40
    }
41
42
    /**
43
     * Sets a parameter.
44
     */
45
    public function setParameter(string $name, $value): void
46
    {
47
        $this->validateName($name, true);
48
        $configuration = $this->parseFile();
49
        $configuration['parameters'][$name] = $value;
50
        $this->dumpFile($configuration);
51
    }
52
53
    /**
54
     * Set all the parameters.
55
     */
56
    public function setParameters(array $params = []): void
57
    {
58
        foreach ($params as $name => $value) {
59
            $this->validateName($name, true);
60
        }
61
        $configuration = $this->parseFile();
62
        $configuration['parameters'] = $params;
63
        $this->dumpFile($configuration);
64
    }
65
66
    /**
67
     * Returns a parameter.
68
     *
69
     * @return mixed The parameter value
70
     */
71
    public function getParameter(string $name)
72
    {
73
        $this->validateName($name, true);
74
        $configuration = $this->parseFile();
75
76
        return $configuration['parameters'][$name] ?? null;
77
    }
78
79
    /**
80
     * Return all the parameters.
81
     */
82
    public function getParameters(): array
83
    {
84
        $configuration = $this->parseFile();
85
86
        return $configuration['parameters'] ?? [];
87
    }
88
89
    /**
90
     * Deletes a parameter.
91
     */
92
    public function delParameter(string $name): void
93
    {
94
        $this->validateName($name, true);
95
        $configuration = $this->parseFile();
96
        if (isset($configuration['parameters'][$name])) {
97
            unset($configuration['parameters'][$name]);
98
            $this->dumpFile($configuration);
99
        }
100
    }
101
102
    /**
103
     * Parses a Yaml file and return a configuration array.
104
     */
105
    protected function parseFile(?string $path = null): array
106
    {
107
        $path = $path ?? $this->fullPath;
108
        if (!$this->fs->exists($path)) {
109
            return [];
110
        }
111
112
        return Yaml::parse(file_get_contents($path));
113
    }
114
115
    /**
116
     * Dump configuration into dynamic configuration file.
117
     */
118
    protected function dumpFile(array $configuration = []): void
119
    {
120
        $yaml = Yaml::dump($configuration);
121
        $this->fs->dumpFile($this->fullPath, $yaml);
122
    }
123
124
    public function deleteFile(): void
125
    {
126
        $this->fs->remove($this->fullPath);
127
    }
128
129
    /**
130
     * Validates that the name is correct.
131
     *
132
     * @throws InvalidArgumentException Thrown if the name is invalid
133
     */
134
    protected function validateName(string $name, bool $isParameter): void
135
    {
136
        if (!is_string($name) || (!$isParameter && 'parameters' === $name) || mb_strlen($name) <= 0) {
0 ignored issues
show
introduced by
The condition is_string($name) is always true.
Loading history...
137
            if ($isParameter) {
138
                throw new InvalidArgumentException('The parameter name must be a string');
139
            }
140
            throw new InvalidArgumentException('The configuration name must not be "parameters" and must be a string');
141
        }
142
    }
143
}
144