Notifier   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 60
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A notify() 0 4 1
A afterStep() 0 14 2
A spamNotify() 0 13 3
A afterSuite() 0 10 2
1
<?php
2
3
namespace Behatch\Notifier;
4
5
use Behat\Behat\Context\Context;
6
use Behat\Behat\Tester\Result\StepResult;
7
8
abstract class Notifier implements Context
9
{
10
    protected static $spamTimeout;
11
12
    public function __construct($spamTimeout = 60)
13
    {
14
        self::$spamTimeout = $spamTimeout;
15
    }
16
17
    protected static function notify($status, $title, $message)
18
    {
19
        throw new \LogicException('You should implement notify() method in your class');
20
    }
21
22
    /**
23
     * @AfterStep
24
     */
25
    public function afterStep($event)
26
    {
27
        $result = $event->getTestResult();
28
29
        if ($result->getResultCode() === StepResult::FAILED) {
30
            $definition = $result->getStepDefinition();
31
32
            $message = "$definition\n";
33
            $message .= '> ' . $result->getException()->getMessage();
34
            $message = str_replace("'", "`", $message);
35
36
            self::spamNotify('error', 'Behat step failure', $message);
37
        }
38
    }
39
40
    private static function spamNotify($status, $title, $message)
41
    {
42
        static $lastTimeError = null;
43
44
        if ($lastTimeError === null) {
45
            $lastTimeError = time() - self::$spamTimeout - 100;
46
        }
47
48
        if (time() - $lastTimeError > self::$spamTimeout) {
49
            static::notify($status, $title, $message);
50
            $lastTimeError = time();
51
        }
52
    }
53
54
    /**
55
     * @AfterSuite
56
     */
57
    public static function afterSuite($event)
58
    {
59
        $result = $event->getTestResult();
60
61
        if ($result->getResultCode() === StepResult::FAILED) {
62
            static::notify('sad', 'Behat suite ended', 'Failure');
63
        } else {
64
            static::notify('smile', 'Behat suite ended', 'Success');
65
        }
66
    }
67
}
68