Passed
Pull Request — master (#17)
by Yo
02:36
created

KernelFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 79
rs 10
c 0
b 0
f 0
ccs 24
cts 27
cp 0.8889

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B load() 0 31 2
1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\Factory;
3
4
use Symfony\Component\HttpKernel\KernelInterface;
5
use Yoanm\Behat3SymfonyExtension\Dispatcher\BehatKernelEventDispatcher;
6
7
class KernelFactory
8
{
9
    const KERNEL_BRIDGE_CLASS_NAME = 'YoanmBehat3SymfonyKernelBridge';
10
    const KERNEL_BRIDGE_TEMPLATE_COMMENT = <<<COMMENT
11
12
/******** WARNING : THIS FILE IS JUST A TEMPLATE, IT IS NOT LOADABLE AS IS ********/
13
COMMENT;
14
15
    /** @var BehatKernelEventDispatcher */
16
    private $behatKernelEventDispatcher;
17
    /** @var string */
18
    private $originalKernelPath;
19
    /** @var string */
20
    private $originalKernelClassName;
21
22
    /** @var string  */
23
    private $kernelEnvironment;
24
    /** @var bool */
25
    private $kernelDebug;
26
27
28
    /**
29
     * @param BehatKernelEventDispatcher $behatKernelEventDispatcher
30
     * @param string                     $originalKernelPath
31
     * @param string                     $originalKernelClassName
32
     * @param string                     $kernelEnvironment
33
     * @param bool                       $kernelDebug
34
     */
35 1
    public function __construct(
36
        BehatKernelEventDispatcher $behatKernelEventDispatcher,
37
        $originalKernelPath,
38
        $originalKernelClassName,
39
        $kernelEnvironment,
40
        $kernelDebug
41
    ) {
42 1
        $this->originalKernelPath = $originalKernelPath;
43 1
        $this->originalKernelClassName = $originalKernelClassName;
44 1
        $this->kernelEnvironment = $kernelEnvironment;
45 1
        $this->kernelDebug = $kernelDebug;
46 1
        $this->behatKernelEventDispatcher = $behatKernelEventDispatcher;
47 1
    }
48
49
    /**
50
     * @return KernelInterface
51
     *
52
     * @throws \Exception
53
     */
54 1
    public function load()
55
    {
56
        // Write the custom kernel file at same level than original one for autoloading purpose
57 1
        $kernelBridgeClassName = self::KERNEL_BRIDGE_CLASS_NAME;
58 1
        $originAppKernelDir = dirname($this->originalKernelPath);
59 1
        $customAppKernelPath = sprintf('%s/%s.php', $originAppKernelDir, $kernelBridgeClassName);
60
        try {
61
            /* /!\ YoanmBehat3SymfonyKernelBridge.php is just template file /!\ */
62 1
            $template = file_get_contents(__DIR__.'/../Bridge/YoanmBehat3SymfonyKernelBridge.php');
63 1
            file_put_contents(
64 1
                $customAppKernelPath,
65 1
                $template = str_replace(
66 1
                    ['OriginalKernelClassNameToReplace', self::KERNEL_BRIDGE_TEMPLATE_COMMENT],
67 1
                    [$this->originalKernelClassName, ''],
68
                    $template
69 1
                )
70 1
            );
71
72 1
            require($customAppKernelPath);
73 1
            unlink($customAppKernelPath);
74
75 1
            $kernelBridge = new $kernelBridgeClassName($this->kernelEnvironment, $this->kernelDebug);
76 1
            $kernelBridge->setBehatKernelEventDispatcher($this->behatKernelEventDispatcher);
77
78 1
            return $kernelBridge;
79
        } catch (\Exception $e) {
80
            unlink($customAppKernelPath);
81
82
            throw new \Exception('An exception occured during Kernel decoration : '.$e->getMessage(), 0, $e);
83
        }
84
    }
85
}
86