Completed
Push — ezp26175-exception_on_non_defa... ( 77d2f3...ca5fc8 )
by
unknown
39:33
created

YamlConfigurationContext::addConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 9.4285
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\Component\Yaml\Yaml;
10
11
/**
12
 * Adds extra YAML configuration through ezplatform_behat.yml.
13
 *
14
 * New configuration blocks are added to unique files, and added to the imports.
15
 * Existing configuration strings re-use the same file if applicable.
16
 */
17
class YamlConfigurationContext implements Context
18
{
19
    private static $platformConfigurationFilePath = 'app/config/ezplatform_behat.yml';
20
21
    public function addConfiguration(array $configuration)
22
    {
23
        $yamlString = Yaml::dump($configuration, 5, 4);
24
        $destinationFileName = 'ezplatform_behat_' . sha1($yamlString) . '.yml';
25
        $destinationFilePath = 'app/config/' . $destinationFileName;
26
27
        if (!file_exists($destinationFilePath)) {
28
            file_put_contents($destinationFilePath, $yamlString);
29
        }
30
31
        $this->addImportToPlatformYaml($destinationFileName);
32
33
        shell_exec('php app/console --env=behat cache:clear');
34
    }
35
36
    private function addImportToPlatformYaml($importedFileName)
37
    {
38
        $platformConfig = Yaml::parse(file_get_contents(self::$platformConfigurationFilePath));
39
40
        foreach ($platformConfig['imports'] as $import) {
41
            if ($import['resource'] == $importedFileName) {
42
                $importAlreadyExists = true;
43
            }
44
        }
45
46
        if (!isset($importAlreadyExists)) {
47
            $platformConfig['imports'][] = ['resource' => $importedFileName];
48
49
            file_put_contents(
50
                self::$platformConfigurationFilePath,
51
                Yaml::dump($platformConfig, 5, 4)
52
            );
53
        }
54
    }
55
}
56