NotifyTrait::debugNotify()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Webino (http://webino.sk/)
4
 *
5
 * @link        https://github.com/webino/WebinoDev/ for the canonical source repository
6
 * @copyright   Copyright (c) 2017-2018 Webino, s. r. o. (http://webino.sk/)
7
 * @license     BSD-3-Clause
8
 */
9
10
namespace WebinoDev\Test\Selenium;
11
12
/**
13
 * Raise client-side notifications
14
 *
15
 * @property-read object $session
16
 */
17
trait NotifyTrait
18
{
19
    /**
20
     * @param bool $withDataSet
21
     * @return string
22
     */
23
    abstract public function getName($withDataSet = true);
24
25
    /**
26
     * @return object|\PHPWebDriver_WebDriverSession
27
     */
28
    abstract protected function getSession();
29
30
    /**
31
     * @param string $msg
32
     * @return $this
33
     */
34
    protected function notifyInfo($msg)
35
    {
36
        $this->debugNotify($msg, 5000);
37
        return $this;
38
    }
39
40
    /**
41
     * @param string $msg
42
     * @return $this
43
     */
44
    protected function notifyError($msg)
45
    {
46
        $this->debugNotify($msg, 5000, 'error');
47
        return $this;
48
    }
49
50
    /**
51
     * @param string $msg
52
     * @param int $timeout
53
     * @param string $type
54
     * @return $this
55
     */
56
    protected function debugNotify($msg, $timeout = 2000, $type = 'info')
57
    {
58
        if (empty($this->session)) {
59
            // fail silently
60
            return $this;
61
        }
62
63
        $title = get_class($this) . '::' . $this->getName(false);
64
        $msg   = trim(addslashes(preg_replace('~[[:space:]]+~', ' ', nl2br($msg))));
65
        $args  = '"' . $title . '", "' . $msg . '", ' . $timeout . ', "' . $type . '"';
66
        $cmd   = '("function" === typeof _debugNotify) && _debugNotify(' . $args . ');';
67
68
        $this->session->execute(['script' => $cmd, 'args' => []]);
69
        sleep($timeout / 1000);
70
        return $this;
71
    }
72
}
73