Completed
Push — master ( 3b1951...58d3fb )
by Lexey
02:23
created

Processor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace LF\EnvDiff;
4
5
use InvalidArgumentException;
6
use LF\EnvDiff\IO\IOInterface;
7
use RuntimeException;
8
9
class Processor
10
{
11
    /** @var IOInterface */
12
    private $io;
13
14
    /**
15
     * Processor constructor.
16
     *
17
     * @param IOInterface $io
18
     */
19 30
    public function __construct(IOInterface $io)
20
    {
21 30
        $this->io = $io;
22 30
    }
23
24
    /**
25
     * @param Config $config
26
     *
27
     * @throws InvalidArgumentException
28
     * @throws RuntimeException
29
     *
30
     * @return bool
31
     */
32 20
    public function actualizeEnv(Config $config)
33
    {
34 20
        $dist   = $config->getDist();
35 20
        $target = $config->getTarget();
36 20
        $exists = is_file($target);
37
38 20
        $this->io->write(sprintf('Actualize env from %s', $dist));
39
40
        try {
41 20
            $distEnv   = Env::parse($dist);
42 17
            $actualEnv = $exists ? Env::parse($target) : [];
43 20
        } catch (InvalidArgumentException $exception) {
44 3
            $this->io->write(sprintf('<error>%s, abort</error>', $exception->getMessage()));
45
46 3
            return true;
47
        }
48
49 17
        $actualEnv = $this->processEnv($distEnv, $actualEnv, $config->isKeepOutdatedEnv());
50
51 17
        if (!is_dir($dir = dirname($target))) {
52 2
            mkdir($dir, 0755, true);
53 2
        }
54
55 17
        ksort($actualEnv);
56 17
        file_put_contents(
57 17
            $target,
58 17
            '# This file is auto-generated during the composer install' . PHP_EOL . Env::dump($actualEnv)
59 17
        );
60
61 17
        $this->io->write(sprintf('<info>%s has been %s</info>', $target, $exists ? 'updated' : 'created'));
62
63 17
        return false;
64
    }
65
66
    /**
67
     * @param Config $config
68
     *
69
     * @throws InvalidArgumentException
70
     *
71
     * @return bool
72
     */
73 10
    public function showDifference(Config $config)
74
    {
75 10
        $dist   = $config->getDist();
76 10
        $target = $config->getTarget();
77
78
        try {
79 10
            $distEnv   = Env::parse($dist);
80 7
            $actualEnv = Env::parse($target);
81 10
        } catch (InvalidArgumentException $exception) {
82 4
            $this->io->write(sprintf('<error>%s</error>', $exception->getMessage()));
83
84 4
            return true;
85
        }
86
87 6
        $extraEnv   = array_diff_key($actualEnv, $distEnv);
88 6
        $missingEnv = array_diff_key($distEnv, $actualEnv);
89 6
        $changedEnv = array_diff(array_intersect_key($distEnv, $actualEnv), $actualEnv);
90
91 6
        if (!count($missingEnv) && !count($extraEnv) && !count($changedEnv)) {
92 2
            $this->io->write(sprintf('<info>%s and %s is identical</info>', $target, $dist));
93
94 2
            return false;
95
        }
96
97 4
        $this->io->write(sprintf('Diff between %s and %s files:', $target, $dist));
98 4
        $this->io->write('');
99
100 4
        foreach ($missingEnv as $env => $value) {
101 2
            $this->io->write(sprintf('<fg=red>- %s=%s</>', $env, $value));
102 4
        }
103 4
        foreach ($extraEnv as $env => $value) {
104 2
            $this->io->write(sprintf('<fg=green>+ %s=%s</>', $env, $value));
105 4
        }
106 4
        foreach ($changedEnv as $env => $default) {
107 2
            $this->io->write(sprintf('<fg=cyan>@ %s=%s (%s)</>', $env, $actualEnv[$env], $default));
108 4
        }
109
110 4
        return false;
111
    }
112
113
    /**
114
     * @param array $expectedEnv
115
     * @param array $actualEnv
116
     * @param bool  $keepOutdated
117
     *
118
     * @return array
119
     *
120
     * @throws RuntimeException
121
     */
122 17
    private function processEnv(array $expectedEnv, array $actualEnv, $keepOutdated)
123
    {
124 17
        if (false === $keepOutdated) {
125 2
            $actualEnv = array_intersect_key($actualEnv, $expectedEnv);
126 2
        }
127
128 17
        return $this->getEnv($expectedEnv, $actualEnv);
129
    }
130
131
    /**
132
     * @param array $expectedEnv
133
     * @param array $actualEnv
134
     *
135
     * @return array
136
     *
137
     * @throws RuntimeException
138
     */
139 17
    private function getEnv(array $expectedEnv, array $actualEnv)
140
    {
141 17
        if (!$this->io->isInteractive()) {
142 11
            return array_replace($expectedEnv, $actualEnv);
143
        }
144
145 6
        $diffEnv = array_diff_key($expectedEnv, $actualEnv);
146 6
        if (count($diffEnv) > 0) {
147 3
            $this->io->write('<comment>Some env variables are missing. Please provide them.</comment>');
148
149 3
            foreach ($diffEnv as $env => $default) {
150 3
                $actualEnv[$env] = $this->io->ask(
151 3
                    sprintf('<question>%s</question> (<comment>%s</comment>): ', $env, $default),
152
                    $default
153 3
                );
154 3
            }
155 3
        }
156
157 6
        return $actualEnv;
158
    }
159
}
160