Skip to content

Commit 5c58d79

Browse files
committed
#12 : refacto
1 parent de6bd0a commit 5c58d79

File tree

5 files changed

+88
-46
lines changed

5 files changed

+88
-46
lines changed

Diff for: src/Domain/Environment/DTO/Context.php

+18-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Domain\Environment\DTO;
46

7+
use Kiboko\Cloud\Domain\Environment\VariableNotFoundException;
58
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
69
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
710
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
@@ -18,32 +21,22 @@ public function __construct(?Deployment $deployment = null)
1821
$this->environmentVariables = [];
1922
}
2023

21-
public function addVariables(EnvironmentVariableInterface ...$variables): void
24+
public function addVariable(EnvironmentVariableInterface ...$variable): void
2225
{
23-
array_push($this->environmentVariables, ...$variables);
26+
array_push($this->environmentVariables, ...$variable);
2427
}
2528

26-
public function getVariableValue(string $variableName)
29+
public function getVariable(string $variableName): EnvironmentVariableInterface
2730
{
2831
foreach ($this->environmentVariables as $variable) {
29-
if ($variable instanceof DirectValueEnvironmentVariable) {
30-
if ($variableName === $variable->getVariable()->__toString()) {
31-
$value = $variable->getValue()->__toString();
32-
if (!$value) {
33-
$value = ' ';
34-
}
35-
return $value;
36-
}
37-
} else if ($variable instanceof SecretValueEnvironmentVariable) {
38-
if ($variableName === $variable->getVariable()->__toString()) {
39-
return '**secret**';
40-
}
41-
} else {
42-
if ($variableName === $variable->getVariable()->__toString()) {
43-
return ' ';
44-
}
32+
if ($variableName !== (string) $variable->getVariable()) {
33+
continue;
4534
}
35+
36+
return $variable;
4637
}
38+
39+
throw new VariableNotFoundException(strtr('The variable %name% does not exist.', ['%name%' => $variableName]));
4740
}
4841

4942
public function denormalize(DenormalizerInterface $denormalizer, $data, string $format = null, array $context = [])
@@ -52,13 +45,13 @@ public function denormalize(DenormalizerInterface $denormalizer, $data, string $
5245
$this->environmentVariables = [];
5346

5447
$parser = new ExpressionParser();
55-
foreach ($data['environmentVariables'] as $variable) {
48+
foreach ($data['environment'] as $variable) {
5649
if (isset($variable['value'])) {
5750
$this->environmentVariables[] = new DirectValueEnvironmentVariable(
5851
new Variable($variable['name']),
5952
$parser->parse($variable['value'])
6053
);
61-
} else if (isset($variable['secret'])) {
54+
} elseif (isset($variable['secret'])) {
6255
$this->environmentVariables[] = new SecretValueEnvironmentVariable(
6356
new Variable($variable['name']),
6457
$variable['secret']
@@ -75,15 +68,15 @@ public function normalize(NormalizerInterface $normalizer, string $format = null
7568
{
7669
return [
7770
'deployment' => $normalizer->normalize($this->deployment, $format, $context),
78-
'environment' => (function($variables) {
71+
'environment' => iterator_to_array((function ($variables) {
7972
/** @var EnvironmentVariableInterface $variable */
8073
foreach ($variables as $variable) {
8174
if ($variable instanceof DirectValueEnvironmentVariable) {
8275
yield [
8376
'name' => (string) $variable->getVariable(),
8477
'value' => $variable->getValue(),
8578
];
86-
} else if ($variable instanceof SecretValueEnvironmentVariable){
79+
} elseif ($variable instanceof SecretValueEnvironmentVariable) {
8780
yield [
8881
'name' => (string) $variable->getVariable(),
8982
'secret' => $variable->getSecret(),
@@ -94,7 +87,7 @@ public function normalize(NormalizerInterface $normalizer, string $format = null
9487
];
9588
}
9689
}
97-
})($this->environmentVariables),
90+
})($this->environmentVariables)),
9891
];
9992
}
10093
}

Diff for: src/Domain/Environment/VariableNotFoundException.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Kiboko\Cloud\Domain\Environment;
4+
5+
class VariableNotFoundException extends \RuntimeException
6+
{
7+
8+
}

Diff for: src/Platform/Console/Command/Environment/InitCommand.php

+27-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Platform\Console\Command\Environment;
46

57
use Kiboko\Cloud\Domain\Environment\DTO\Context;
68
use Kiboko\Cloud\Domain\Environment\DTO\Deployment;
9+
use Kiboko\Cloud\Domain\Environment\DTO\DirectValueEnvironmentVariable;
10+
use Kiboko\Cloud\Domain\Environment\DTO\SecretValueEnvironmentVariable;
711
use Kiboko\Cloud\Domain\Environment\DTO\Server;
12+
use Kiboko\Cloud\Domain\Environment\DTO\Variable;
813
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
914
use Symfony\Component\Console\Command\Command;
1015
use Symfony\Component\Console\Input\InputInterface;
1116
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Question\ConfirmationQuestion;
1218
use Symfony\Component\Console\Question\Question;
1319
use Symfony\Component\Console\Style\SymfonyStyle;
1420
use Symfony\Component\Finder\Finder;
1521
use Symfony\Component\Serializer\Encoder\YamlEncoder;
22+
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
1623
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
1724
use Symfony\Component\Serializer\Serializer;
1825

@@ -48,17 +55,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
4855

4956
$serializer = new Serializer(
5057
[
58+
new CustomNormalizer(),
5159
new PropertyNormalizer(),
5260
],
5361
[
54-
new YamlEncoder()
62+
new YamlEncoder(),
5563
]
5664
);
5765

5866
if ($finder->hasResults()) {
5967
/** @var \SplFileInfo $file */
6068
foreach ($finder->name('/^\.?kloud.environment.ya?ml$/') as $file) {
6169
$format->error('The directory was already initialized with an environment file. You should update it using commands listed in environment:variable');
70+
6271
return 0;
6372
}
6473
}
@@ -76,21 +85,30 @@ protected function execute(InputInterface $input, OutputInterface $output)
7685
),
7786
);
7887

79-
$envDistPath = getcwd() . '/.env.dist';
88+
$envDistPath = getcwd().'/.env.dist';
8089
if (file_exists($envDistPath)) {
8190
$envDist = parse_ini_file($envDistPath);
8291
foreach (array_keys($envDist) as $name) {
83-
$value = $format->askQuestion(new Question('Value of ' . $name));
84-
$variable = [$name, $value];
85-
$context->environmentVariables[$name] = $value;
92+
$value = $format->askQuestion(new Question('Value of '.$name));
93+
94+
$isSecret = false;
95+
if ($value) {
96+
$isSecret = $format->askQuestion(new ConfirmationQuestion('Is this a secret variable ?', false));
97+
}
98+
99+
if ($isSecret) {
100+
$context->addVariable(new SecretValueEnvironmentVariable(new Variable($name), $value));
101+
} else {
102+
$context->addVariable(new DirectValueEnvironmentVariable(new Variable($name), $value));
103+
}
86104
}
87105
}
88106

89107
$format->note('Writing a new .kloud.environment.yaml file.');
90-
file_put_contents($workingDirectory . '/.kloud.environment.yaml', $serializer->serialize($context, 'yaml', [
91-
'yaml_inline' => 2,
108+
file_put_contents($workingDirectory.'/.kloud.environment.yaml', $serializer->serialize($context, 'yaml', [
109+
'yaml_inline' => 4,
92110
'yaml_indent' => 0,
93-
'yaml_flags' => 0
111+
'yaml_flags' => 0,
94112
]));
95113

96114
return 0;

Diff for: src/Platform/Console/Command/Environment/Variable/GetCommand.php

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Platform\Console\Command\Environment\Variable;
46

57
use Kiboko\Cloud\Domain\Environment\DTO\Context;
8+
use Kiboko\Cloud\Domain\Environment\DTO\SecretValueEnvironmentVariable;
9+
use Kiboko\Cloud\Domain\Environment\DTO\ValuedEnvironmentVariableInterface;
10+
use Kiboko\Cloud\Domain\Environment\VariableNotFoundException;
11+
use Kiboko\Cloud\Domain\Stack\Compose\EnvironmentVariableInterface;
612
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
713
use Symfony\Component\Console\Command\Command;
814
use Symfony\Component\Console\Input\InputInterface;
@@ -52,15 +58,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
5258
new PropertyNormalizer(),
5359
],
5460
[
55-
new YamlEncoder()
61+
new YamlEncoder(),
5662
]
5763
);
5864

5965
if ($finder->hasResults()) {
6066
/** @var SplFileInfo $file */
6167
foreach ($finder->name('/^\.?kloud.environment.ya?ml$/') as $file) {
6268
try {
63-
/** @var \Kiboko\Cloud\Domain\Stack\DTO\Context $context */
69+
/** @var Context $context */
6470
$context = $serializer->deserialize($file->getContents(), Context::class, 'yaml');
6571
} catch (\Throwable $exception) {
6672
$format->error($exception->getMessage());
@@ -73,23 +79,32 @@ protected function execute(InputInterface $input, OutputInterface $output)
7379

7480
if (!isset($context)) {
7581
$format->error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command');
82+
7683
return 1;
7784
}
7885

7986
$variableName = $format->askQuestion(new Question('Please enter a variable name'));
8087

81-
/** @var Context $context */
82-
$value = $context->getVariableValue($variableName);
88+
try {
89+
/** @var EnvironmentVariableInterface $variable */
90+
$variable = $context->getVariable($variableName);
91+
} catch (VariableNotFoundException $exception) {
92+
$format->error($exception->getMessage());
8393

84-
if (!$value) {
85-
$format->error('This variable does not exist');
86-
return 0;
94+
return 1;
8795
}
8896

8997
$format->table(
9098
['Variable', 'Value'],
9199
[
92-
[$variableName, $value],
100+
[
101+
$variableName,
102+
$variable instanceof ValuedEnvironmentVariableInterface ?
103+
$variable->getValue() :
104+
($variable instanceof SecretValueEnvironmentVariable ?
105+
sprintf('SECRET: %s', $variable->getSecret()) :
106+
null),
107+
],
93108
]
94109
);
95110

Diff for: src/Platform/Console/Command/Environment/Variable/ListCommand.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
<?php declare(strict_types=1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace Kiboko\Cloud\Platform\Console\Command\Environment\Variable;
46

57
use Kiboko\Cloud\Domain\Environment\DTO\Context;
68
use Kiboko\Cloud\Domain\Environment\DTO\EnvironmentVariableInterface;
9+
use Kiboko\Cloud\Domain\Environment\DTO\SecretValueEnvironmentVariable;
710
use Kiboko\Cloud\Domain\Environment\DTO\ValuedEnvironmentVariableInterface;
811
use Kiboko\Cloud\Platform\Console\EnvironmentWizard;
912
use Symfony\Component\Console\Command\Command;
@@ -53,7 +56,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5356
new PropertyNormalizer(),
5457
],
5558
[
56-
new YamlEncoder()
59+
new YamlEncoder(),
5760
]
5861
);
5962

@@ -74,6 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7477

7578
if (!isset($context)) {
7679
$format->error('No .kloud.environment.yaml file found in your directory. You must initialize it using environment:init command');
80+
7781
return 1;
7882
}
7983

@@ -84,7 +88,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
8488
foreach ($context->environmentVariables as $variable) {
8589
yield [
8690
(string) $variable->getVariable(),
87-
($context->getVariableValue((string) $variable->getVariable()))
91+
$variable instanceof ValuedEnvironmentVariableInterface ?
92+
$variable->getValue() :
93+
($variable instanceof SecretValueEnvironmentVariable ?
94+
sprintf('SECRET: %s', $variable->getSecret()) :
95+
null),
8896
];
8997
}
9098
})($context)),

0 commit comments

Comments
 (0)