StepTimeLoggerListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bex\Behat\StepTimeLoggerExtension\Listener;
4
5
use Behat\Behat\EventDispatcher\Event\AfterStepTested;
6
use Behat\Behat\EventDispatcher\Event\BeforeStepTested;
7
use Behat\Behat\EventDispatcher\Event\StepTested;
8
use Behat\Testwork\EventDispatcher\Event\AfterSuiteTested;
9
use Behat\Testwork\EventDispatcher\Event\SuiteTested;
10
use Bex\Behat\StepTimeLoggerExtension\ServiceContainer\Config;
11
use Bex\Behat\StepTimeLoggerExtension\Service\StepTimeLogger;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
final class StepTimeLoggerListener implements EventSubscriberInterface
15
{
16
    /**
17
     * @var Config
18
     */
19
    private $config;
20
21
    /**
22
     * @var StepTimeLogger
23
     */
24
    private $stepTimeLogger;
25
26
    /**
27
     * @param Config         $config
28
     * @param StepTimeLogger $stepTimeLogger
29
     */
30
    public function __construct(Config $config, StepTimeLogger $stepTimeLogger)
31
    {
32
        $this->config = $config;
33
        $this->stepTimeLogger = $stepTimeLogger;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public static function getSubscribedEvents()
40
    {
41
        return [
42
            StepTested::BEFORE => 'stepStarted',
43
            StepTested::AFTER => 'stepFinished',
44
            SuiteTested::AFTER => 'suiteFinished'
45
        ];
46
    }
47
48
    /**
49
     * @param BeforeStepTested $event
50
     */
51
    public function stepStarted(BeforeStepTested $event)
52
    {
53
        if ($this->config->isEnabled()) {
54
            $this->stepTimeLogger->logStepStarted($event->getStep()->getText());
55
        }
56
    }
57
58
    /**
59
     * @param AfterStepTested $event
60
     */
61
    public function stepFinished(AfterStepTested $event)
62
    {
63
        if ($this->config->isEnabled()) {
64
            $this->stepTimeLogger->logStepFinished($event->getStep()->getText());
65
        }
66
    }
67
68
    /**
69
     * @return void
70
     */
71
    public function suiteFinished()
72
    {
73
        if ($this->config->isEnabled()) {
74
            foreach ($this->config->getOutputPrinters() as $printer) {
75
                $printer->printLogs($this->stepTimeLogger->executionInformationGenerator());
76
            }
77
78
            $this->stepTimeLogger->clearLogs();
79
        }
80
    }
81
}
82