IncenteevParametersProcessor   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 9
dl 0
loc 155
ccs 0
cts 95
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C process() 0 41 8
B processFile() 0 31 6
A writeOutputFile() 0 21 3
A processEnvironmentalVariables() 0 12 3
1
<?php
2
3
namespace Paro\EnvironmentParameters;
4
5
use Composer\IO\IOInterface;
6
use Composer\Script\Event;
7
use Incenteev\ParameterHandler\Processor;
8
use Paro\EnvironmentParameters\Adapter\Output\PHPConstantsOutputAdapter;
9
use Paro\EnvironmentParameters\Adapter\Output\YamlOutputAdapter;
10
use Symfony\Component\Filesystem\Filesystem;
11
use Symfony\Component\Yaml\Parser;
12
use Symfony\Component\Yaml\Yaml;
13
14
class IncenteevParametersProcessor
15
{
16
    private static $PARAMETER_KEY = null;
17
18
    /**
19
     * @var FileHandler
20
     */
21
    private $fileHandler;
22
    /**
23
     * @var IOInterface
24
     */
25
    private $io;
26
    /**
27
     * @var Filesystem
28
     */
29
    private $fs;
30
31
    /**
32
     * IncenteevParametersProcessor constructor.
33
     * @param Filesystem $fs
34
     * @param FileHandler $fileHandler
35
     * @param IOInterface $io
36
     */
37
    public function __construct(Filesystem $fs, FileHandler $fileHandler, IOInterface $io)
38
    {
39
        $this->fileHandler = $fileHandler;
40
        $this->io = $io;
41
        $this->fs = $fs;
42
    }
43
44
    /**
45
     * @param $configs
46
     * @param Event $event
47
     * @return bool
48
     */
49
    public function process($configs, Event $event)
50
    {
51
        $this->fs->remove($configs['build-folder']);
52
53
        if (!isset($configs['incenteev-parameters'])) {
54
            return true;
55
        }
56
57
        $processor = new Processor($event->getIO());
58
        $parameters = $configs['incenteev-parameters'];
59
        if (array_keys($parameters) !== range(0, count($parameters) - 1)) {
60
            $parameters = array($parameters);
61
        }
62
63
        if (empty($parameters['parameter-key'])) {
64
            self::$PARAMETER_KEY = 'parameters';
65
        } else {
66
            self::$PARAMETER_KEY = $parameters['parameter-key'];
67
        }
68
69
        foreach ($parameters as $config) {
70
            if (!is_array($config)) {
71
                throw new \InvalidArgumentException('The extra.environment-parameters.incenteev-parameters setting must be an array of configuration objects.');
72
            }
73
74
            $file = $this->fileHandler->findFile($config['file']);
75
76
            $outputFileName = $this->fileHandler->preparePath($configs['build-folder'] . '/' . (isset($config['name']) ? $config['name'] : $file));
77
            $this->processFile($file, $outputFileName . '.dist');
78
79
            $config['dist-file'] = $outputFileName . '.dist';
80
            $config['file'] = $outputFileName;
81
            $processor->processFile($config);
82
            $this->fs->remove($outputFileName . '.dist');
83
84
            $outputFormat = isset($config['output-format']) ? $config['output-format'] : YamlOutputAdapter::getName();
85
            $this->writeOutputFile($outputFileName, $outputFormat);
86
        }
87
88
        return true;
89
    }
90
91
    /**
92
     * @param $inFile
93
     * @param string|null $outFile
94
     * @param array $stack
95
     * @return array|bool|mixed
96
     */
97
    public function processFile($inFile, $outFile = null, array $stack = array())
98
    {
99
        $yamlParser = new Parser();
100
        $values = $yamlParser->parse(file_get_contents($this->fileHandler->findFile($inFile)));
101
102
        $values[self::$PARAMETER_KEY] = $this->processEnvironmentalVariables($values[self::$PARAMETER_KEY]);
103
104
        if (isset($values['imports']) && is_array($values['imports'])) {
105
            foreach ($values['imports'] as $importFile) {
106
                $filePath = $this->fileHandler->resolvePath($inFile, $importFile['resource']);
107
                $filePathFull = realpath($filePath);
108
                if (in_array($filePathFull, $stack)) {
109
                    $this->io->write(sprintf('<error>Skipping cyclic import in "%s" of the "%s" file</error>', $inFile, $filePath));
110
                    continue;
111
                }
112
                $stack[] = $filePathFull;
113
                $parametersFromFile = $this->processFile($filePath, null, $stack);
114
                $values = array_replace_recursive($parametersFromFile, $values);
115
            }
116
            unset($values['imports']);
117
        }
118
119
        if (!is_null($outFile)) {
120
            ksort($values[self::$PARAMETER_KEY]);
121
            $this->fs->dumpFile($outFile, Yaml::dump($values));
122
        } else {
123
            return $values;
124
        }
125
126
        return true;
127
    }
128
129
    /**
130
     * @param $file
131
     * @param $outputFormat
132
     * @return mixed
133
     */
134
    protected function writeOutputFile($file, $outputFormat)
135
    {
136
        $yamlParser = new Parser();
137
        $values = $yamlParser->parse(file_get_contents($file));
138
        $values = $values[self::$PARAMETER_KEY];
139
        $env = $this->fileHandler->getArgumentValue('env');
140
        $outputFormat = strtolower($outputFormat);
141
142
        $supportedAdapters = array(
143
            new YamlOutputAdapter(self::$PARAMETER_KEY),
144
            new PHPConstantsOutputAdapter(),
145
        );
146
147
        foreach ($supportedAdapters as $adapter) {
148
            if (strtolower($adapter->getName()) === $outputFormat) {
149
                return $adapter->process($values, $file, $env, time());
150
            }
151
        }
152
153
        throw new \InvalidArgumentException(sprintf('Adapter "%s" doesn\'t exist', $outputFormat));
154
    }
155
156
    protected function processEnvironmentalVariables(array $parameters)
157
    {
158
        $processed = array();
159
        foreach ($parameters as $key => $parameter) {
160
            if (!is_string($parameter)) {
161
                $processed[$key] = $parameter;
162
            } else {
163
                $processed[$key] = $this->fileHandler->processEnvironmentalVariable($parameter);
164
            }
165
        }
166
        return $processed;
167
    }
168
}
169