Test Failed
Push — feature/improve ( 4f0633 )
by Yo
02:21
created

KernelFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\Factory;
3
4
use Yoanm\Behat3SymfonyExtension\Dispatcher\BehatKernelEventDispatcher;
5
6
class KernelFactory
7
{
8
    /** @var BehatKernelEventDispatcher */
9
    private $behatKernelEventDispatcher;
10
    /** @var string */
11
    private $originalKernelPath;
12
    /** @var string */
13
    private $originalKernelClassName;
14
15
    /** @var string  */
16
    private $environment;
17
    /** @var bool */
18
    private $debug;
19
20
21
    /**
22
     * @param BehatKernelEventDispatcher $behatKernelEventDispatcher
23
     * @param string                     $originalKernelPath
24
     * @param string                     $originalKernelClassName
25
     * @param string                     $environment
26
     * @param bool                       $debug
27
     */
28
    public function __construct(
29
        BehatKernelEventDispatcher $behatKernelEventDispatcher,
30
        $originalKernelPath,
31
        $originalKernelClassName,
32
        $environment,
33
        $debug
34
    ) {
35
        $this->originalKernelPath = $originalKernelPath;
36
        $this->originalKernelClassName = $originalKernelClassName;
37
        $this->environment = $environment;
38
        $this->debug = $debug;
39
        $this->behatKernelEventDispatcher = $behatKernelEventDispatcher;
40
    }
41
42
    public function load()
43
    {
44
        $className = 'YoanmBehat3SymfonyKernelBridge';
45
        $originalKernelClassName = $this->originalKernelClassName;
46
        $template = <<<TEMPLATE
47
<?php
48
/**
49
 * Autogenerated by Behat3SymfonyExtension.
50
 * Don't touch the content it will be erased !
51
 * See Yoanm\Behat3SymfonyExtension\Factory\KernelFactory::load()
52
 */
53
use Yoanm\Behat3SymfonyExtension\Dispatcher\BehatKernelEventDispatcher;
54
use ${originalKernelClassName} as ${className}BaseKernel;
55
56
class $className extends ${className}BaseKernel
57
{
58
    /** @var BehatKernelEventDispatcher */
59
    private \$behatKernelEventDispatcher;
60
61
    public function setBehatKernelEventDispatcher(BehatKernelEventDispatcher \$behatKernelEventDispatcher)
62
    {
63
        \$this->behatKernelEventDispatcher = \$behatKernelEventDispatcher;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function boot()
70
    {
71
        \$this->behatKernelEventDispatcher->beforeBoot(\$this);
72
73
        parent::boot();
74
75
        \$this->behatKernelEventDispatcher->afterBoot(\$this);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function shutdown()
82
    {
83
        \$this->behatKernelEventDispatcher->beforeShutdown(\$this);
84
85
        parent::shutdown();
86
87
        \$this->behatKernelEventDispatcher->afterShutdown(\$this);
88
    }
89
}
90
91
TEMPLATE;
92
93
        return $this->createAndLoadCustomAppKernel($template, $className);
94
    }
95
96
    /**
97
     * @param $template
98
     * @param $className
99
     * @return mixed
100
     * @throws \Exception
101
     */
102
    protected function createAndLoadCustomAppKernel($template, $className)
103
    {
104
        // Write the custom kernel file at same level than original one for autoloading purpose
105
        $originAppKernelDir = dirname($this->originalKernelPath);
106
        $customAppKernelPath = sprintf('%s/%s.php', $originAppKernelDir, $className);
107
        try {
108
            file_put_contents($customAppKernelPath, $template);
109
110
            require($customAppKernelPath);
111
            unlink($customAppKernelPath);
112
113
            $class = new $className($this->environment, $this->debug);
114
            $class->setBehatKernelEventDispatcher($this->behatKernelEventDispatcher);
115
116
            return $class;
117
        } catch (\Exception $e) {
118
            unlink($customAppKernelPath);
119
            throw new \Exception('An exception occured during Kernel decoration : '.$e->getMessage());
120
        }
121
    }
122
}
123