Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / EventListener / Statistics / StatisticsListener.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Output\Node\EventListener\Statistics;
12
13 use Behat\Behat\Output\Node\Printer\StatisticsPrinter;
14 use Behat\Behat\Output\Statistics\Statistics;
15 use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
16 use Behat\Testwork\Output\Formatter;
17 use Behat\Testwork\Output\Node\EventListener\EventListener;
18 use Symfony\Component\EventDispatcher\Event;
19
20 /**
21  * Collects general suite stats such as time and memory during its execution and prints it afterwards.
22  *
23  * @author Konstantin Kudryashov <ever.zet@gmail.com>
24  */
25 final class StatisticsListener implements EventListener
26 {
27     /**
28      * @var Statistics
29      */
30     private $statistics;
31     /**
32      * @var StatisticsPrinter
33      */
34     private $printer;
35
36     /**
37      * Initializes listener.
38      *
39      * @param Statistics        $statistics
40      * @param StatisticsPrinter $statisticsPrinter
41      */
42     public function __construct(Statistics $statistics, StatisticsPrinter $statisticsPrinter)
43     {
44         $this->statistics = $statistics;
45         $this->printer = $statisticsPrinter;
46     }
47
48     /**
49      * {@inheritdoc}
50      */
51     public function listenEvent(Formatter $formatter, Event $event, $eventName)
52     {
53         $this->startTimerOnBeforeExercise($eventName);
54         $this->printStatisticsOnAfterExerciseEvent($formatter, $eventName);
55     }
56
57     /**
58      * Starts timer on exercise BEFORE event.
59      *
60      * @param string $eventName
61      */
62     private function startTimerOnBeforeExercise($eventName)
63     {
64         if (ExerciseCompleted::BEFORE !== $eventName) {
65             return;
66         }
67
68         $this->statistics->startTimer();
69     }
70
71     /**
72      * Prints statistics on after exercise event.
73      *
74      * @param Formatter $formatter
75      * @param string    $eventName
76      */
77     private function printStatisticsOnAfterExerciseEvent(Formatter $formatter, $eventName)
78     {
79         if (ExerciseCompleted::AFTER !== $eventName) {
80             return;
81         }
82
83         $this->statistics->stopTimer();
84         $this->printer->printStatistics($formatter, $this->statistics);
85     }
86 }