Completed
Push — symfony5 ( 53dd3f...db4714 )
by
unknown
192:14 queued 180:30
created

YamlConfigurationContext::addConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
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 Behat\Symfony2Extension\Context\KernelDictionary;
10
use Symfony\Bundle\FrameworkBundle\Console\Application;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\HttpKernel\KernelInterface;
13
use Symfony\Component\Yaml\Yaml;
14
15
/**
16
 * Adds extra YAML configuration through ezplatform_behat.yml.
17
 *
18
 * New configuration blocks are added to unique files, and added to the imports.
19
 * Existing configuration strings re-use the same file if applicable.
20
 */
21
class YamlConfigurationContext implements Context
22
{
23
    /** @var \Symfony\Component\HttpKernel\KernelInterface */
24
    private $kernel;
25
26
    private static $platformConfigurationFilePath = 'config/packages/%env%/ezplatform.yaml';
27
28
    public function __construct(KernelInterface $kernel)
29
    {
30
        $this->kernel = $kernel;
31
    }
32
33
    public function addConfiguration(array $configuration)
34
    {
35
        $env = $this->kernel->getEnvironment();
36
37
        $yamlString = Yaml::dump($configuration, 5, 4);
38
        $destinationFileName = 'ezplatform_behat_' . sha1($yamlString) . '.yaml';
39
        $destinationFilePath = "config/packages/{$env}/{$destinationFileName}";
40
41
        if (!file_exists($destinationFilePath)) {
42
            file_put_contents($destinationFilePath, $yamlString);
43
        }
44
45
        $this->addImportToPlatformYaml($destinationFileName, $env);
46
47
        $application = new Application($this->kernel);
48
        $application->setAutoExit(false);
49
50
        $input = new ArrayInput([
51
            'command' => 'cache:clear',
52
        ]);
53
54
        $application->run($input);
55
    }
56
57
    private function addImportToPlatformYaml(string $importedFileName, string $env): void
58
    {
59
        $filePath = str_replace('%env%', $env, self::$platformConfigurationFilePath);
60
        $platformConfig = Yaml::parse(file_get_contents($filePath));
61
62
        if (!array_key_exists('imports', $platformConfig)) {
63
            $platformConfig = array_merge(['imports' => []], $platformConfig);
64
        }
65
66
        foreach ($platformConfig['imports'] as $import) {
67
            if ($import['resource'] == $importedFileName) {
68
                $importAlreadyExists = true;
69
            }
70
        }
71
72
        if (!isset($importAlreadyExists)) {
73
            $platformConfig['imports'][] = ['resource' => $importedFileName];
74
75
            file_put_contents(
76
                $filePath,
77
                Yaml::dump($platformConfig, 5, 4)
78
            );
79
        }
80
    }
81
}
82