Completed
Push — master ( cf6f30...545cd2 )
by Yo
02:19
created

Client::rebootKernel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\Client;
3
4
use Psr\Log\LoggerInterface;
5
use Symfony\Bundle\FrameworkBundle\Client as BaseClient;
6
use Symfony\Component\BrowserKit\CookieJar;
7
use Symfony\Component\BrowserKit\History;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use Symfony\Component\HttpKernel\KernelInterface;
10
use Yoanm\Behat3SymfonyExtension\Event\AfterRequestEvent;
11
use Yoanm\Behat3SymfonyExtension\Event\BeforeRequestEvent;
12
use Yoanm\Behat3SymfonyExtension\Event\Events;
13
14
class Client extends BaseClient
15
{
16
    /** @var bool */
17
    private $requestPerformed = false;
18
    /** @var LoggerInterface */
19
    private $logger;
20
    /** @var EventDispatcherInterface */
21
    private $dispatcher;
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function __construct(
27
        KernelInterface $kernel,
28
        LoggerInterface $logger,
29
        EventDispatcherInterface $dispatcher,
30
        array $server,
31
        History $history,
32
        CookieJar $cookieJar
33
    ) {
34
        $this->logger = $logger;
35
        $this->dispatcher = $dispatcher;
36
        parent::__construct($kernel, $server, $history, $cookieJar);
37
        $this->disableReboot();
38
    }
39
40
    /**
41
     * @return boolean
42
     */
43
    public function hasPerformedRequest()
44
    {
45
        return $this->requestPerformed;
46
    }
47
48
    /**
49
     * @return boolean
50
     */
51
    public function resetClient()
52
    {
53
        $this->logger->debug('Resetting client');
54
        $this->requestPerformed = false;
55
        $this->rebootKernel();
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function doRequest($request)
62
    {
63
        if (true === $this->requestPerformed) {
64
            $this->logger->debug('A request has already been performed => reboot kernel');
65
            $this->rebootKernel();
66
        } else {
67
            $this->requestPerformed = true;
68
        }
69
70
        $this->dispatcher->dispatch(
71
            Events::BEFORE_REQUEST,
72
            new BeforeRequestEvent($request)
73
        );
74
        $response = parent::doRequest($request);
75
        $this->dispatcher->dispatch(
76
            Events::AFTER_REQUEST,
77
            new AfterRequestEvent($response)
78
        );
79
80
        return $response;
81
    }
82
83
    protected function rebootKernel()
84
    {
85
        // Reboot sfKernel to avoid parent::doRequest to shutdown it and from Kernel::handle to boot it
86
        // This behavior will allow mocking symfony app container service for instance
87
        $this->getKernel()->shutdown();
88
        $this->getKernel()->boot();
89
    }
90
}
91