Client   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 69
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A resetClient() 0 6 1
A doRequest() 0 21 2
A rebootKernel() 0 7 1
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 4
    public function __construct(
27
        KernelInterface $kernel,
28
        LoggerInterface $logger,
29
        EventDispatcherInterface $dispatcher,
30
        array $server,
31
        History $history,
32
        CookieJar $cookieJar
33
    ) {
34 4
        $this->logger = $logger;
35 4
        $this->dispatcher = $dispatcher;
36 4
        parent::__construct($kernel, $server, $history, $cookieJar);
37 4
        $this->disableReboot();
38 4
    }
39
40
    /**
41
     * @return boolean
42
     */
43 1
    public function resetClient()
44
    {
45 1
        $this->logger->debug('Resetting client');
46 1
        $this->requestPerformed = false;
47 1
        $this->rebootKernel();
48 1
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 3
    protected function doRequest($request)
54
    {
55 3
        if (true === $this->requestPerformed) {
56 1
            $this->logger->debug('A request has already been performed => reboot kernel');
57 1
            $this->rebootKernel();
58 1
        } else {
59 3
            $this->requestPerformed = true;
60
        }
61
62 3
        $this->dispatcher->dispatch(
63 3
            Events::BEFORE_REQUEST,
64 3
            new BeforeRequestEvent($request)
65 3
        );
66 3
        $response = parent::doRequest($request);
67 3
        $this->dispatcher->dispatch(
68 3
            Events::AFTER_REQUEST,
69 3
            new AfterRequestEvent($response)
70 3
        );
71
72 3
        return $response;
73
    }
74
75 2
    protected function rebootKernel()
76
    {
77
        // Reboot sfKernel to avoid parent::doRequest to shutdown it and from Kernel::handle to boot it
78
        // This behavior will allow mocking symfony app container service for instance
79 2
        $this->getKernel()->shutdown();
80 2
        $this->getKernel()->boot();
81 2
    }
82
}
83