SymfonySuiteGenerator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A supportsTypeAndSettings() 0 4 1
A generateSuite() 0 18 3
A mergeDefaultSettings() 0 12 3
1
<?php
2
3
/*
4
 * This file is part of the Behat Symfony2Extension.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Behat\Symfony2Extension\Suite;
12
13
use Behat\Testwork\Suite\Exception\SuiteConfigurationException;
14
use Behat\Testwork\Suite\Generator\SuiteGenerator;
15
use InvalidArgumentException;
16
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
17
use Symfony\Component\HttpKernel\KernelInterface;
18
19
/**
20
 * @author Christophe Coevoet <[email protected]>
21
 */
22
final class SymfonySuiteGenerator implements SuiteGenerator
23
{
24
    private $kernel;
25
    private $pathSuffix;
26
    private $contextClassSuffix;
27
28
    public function __construct(KernelInterface $kernel, $pathSuffix = 'Features', $contextClassSuffix = 'Features\\Context\\FeatureContext')
29
    {
30
        $this->kernel = $kernel;
31
        $this->pathSuffix = '/' . ltrim($pathSuffix, '/' . DIRECTORY_SEPARATOR);
32
        $this->contextClassSuffix = '\\' . ltrim($contextClassSuffix, '\\');
33
    }
34
35
    /**
36
     * Checks if generator support provided suite type and settings.
37
     *
38
     * @param string $type
39
     * @param array  $settings
40
     *
41
     * @return Boolean
42
     */
43
    public function supportsTypeAndSettings($type, array $settings)
44
    {
45
        return 'symfony_bundle' === $type;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function generateSuite($suiteName, array $settings)
52
    {
53
        $bundleName = isset($settings['bundle']) ? $settings['bundle'] : $suiteName;
54
55
        try {
56
            $bundle = $this->kernel->getBundle($bundleName);
57
        } catch (InvalidArgumentException $e) {
58
            throw new SuiteConfigurationException(
59
                sprintf('The bundle "%s" does not exist in the project', $bundleName),
60
                $suiteName,
61
                $e
62
            );
63
        }
64
65
        $settings['bundle'] = $bundleName;
66
67
        return new SymfonyBundleSuite($suiteName, $bundle, $this->mergeDefaultSettings($bundle, $settings));
68
    }
69
70
    private function mergeDefaultSettings(BundleInterface $bundle, array $settings)
71
    {
72
        if (empty($settings['contexts'])) {
73
            $settings['contexts'] = array($bundle->getNamespace() . $this->contextClassSuffix);
74
        }
75
76
        if (empty($settings['paths'])) {
77
            $settings['paths'] = array($bundle->getPath() . $this->pathSuffix);
78
        }
79
80
        return $settings;
81
    }
82
}
83