Completed
Push — symfony5 ( 430b04...3f74ea )
by
unknown
12:52
created

YamlConfigurationContext   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addConfiguration() 0 18 3
A getEnvironment() 0 4 1
A isSymfonyCacheClearRequired() 0 4 1
A addImportToPlatformYaml() 0 24 5
A clearSymfonyCache() 0 13 1
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Bundle\EzPublishCoreBundle\Features\Context;
7
8
use Behat\Behat\Context\Context;
9
use Symfony\Bundle\FrameworkBundle\Console\Application;
10
use Symfony\Component\Console\Input\ArrayInput;
11
use Symfony\Component\HttpKernel\KernelInterface;
12
use Symfony\Component\Yaml\Yaml;
13
14
/**
15
 * Adds extra YAML configuration through ezplatform_behat.yml.
16
 *
17
 * New configuration blocks are added to unique files, and added to the imports.
18
 * Existing configuration strings re-use the same file if applicable.
19
 */
20
class YamlConfigurationContext implements Context
21
{
22
    /** @var \Symfony\Component\HttpKernel\KernelInterface */
23
    private $kernel;
24
25
    private static $platformConfigurationFilePath = 'config/packages/%env%/ezplatform.yaml';
26
27
    public function __construct(KernelInterface $kernel)
28
    {
29
        $this->kernel = $kernel;
30
    }
31
32
    public function addConfiguration(array $configuration)
33
    {
34
        $env = $this->getEnvironment();
35
36
        $yamlString = Yaml::dump($configuration, 5, 4);
37
        $destinationFileName = 'ezplatform_behat_' . sha1($yamlString) . '.yaml';
38
        $destinationFilePath = "config/packages/{$env}/{$destinationFileName}";
39
40
        if (!file_exists($destinationFilePath)) {
41
            file_put_contents($destinationFilePath, $yamlString);
42
        }
43
44
        $this->addImportToPlatformYaml($destinationFileName, $env);
45
46
        if ($this->isSymfonyCacheClearRequired()) {
47
            $this->clearSymfonyCache();
48
        }
49
    }
50
51
    public function getEnvironment(): string
52
    {
53
        return $this->kernel->getEnvironment();
54
    }
55
56
    public function isSymfonyCacheClearRequired(): string
57
    {
58
        return 'prod' === $this->getEnvironment();
59
    }
60
61
    private function addImportToPlatformYaml(string $importedFileName, string $env): void
62
    {
63
        $filePath = str_replace('%env%', $env, self::$platformConfigurationFilePath);
64
        $platformConfig = Yaml::parse(file_get_contents($filePath));
65
66
        if (!array_key_exists('imports', $platformConfig)) {
67
            $platformConfig = array_merge(['imports' => []], $platformConfig);
68
        }
69
70
        foreach ($platformConfig['imports'] as $import) {
71
            if ($import['resource'] == $importedFileName) {
72
                $importAlreadyExists = true;
73
            }
74
        }
75
76
        if (!isset($importAlreadyExists)) {
77
            $platformConfig['imports'][] = ['resource' => $importedFileName];
78
79
            file_put_contents(
80
                $filePath,
81
                Yaml::dump($platformConfig, 5, 4)
82
            );
83
        }
84
    }
85
86
    public function clearSymfonyCache(): void
87
    {
88
        $application = new Application($this->kernel);
89
        $application->setAutoExit(false);
90
91
        $input = new ArrayInput(
92
            [
93
                'command' => 'cache:clear',
94
            ]
95
        );
96
97
        $application->run($input);
98
    }
99
}
100