Completed
Push — develop ( 0f7fe1...c45449 )
by Peter
10:05
created

NotifyTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
getName() 0 1 ?
getSession() 0 1 ?
A notifyInfo() 0 5 1
A notifyError() 0 5 1
A debugNotify() 0 11 1
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) 2016 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
trait NotifyTrait
16
{
17
    /**
18
     * @param bool $withDataSet
19
     * @return string
20
     */
21
    abstract public function getName($withDataSet = true);
22
23
    /**
24
     * @return \PHPWebDriver_WebDriverSession
25
     */
26
    abstract protected function getSession();
27
28
    /**
29
     * @param string $msg
30
     * @return $this
31
     */
32
    protected function notifyInfo($msg)
33
    {
34
        $this->debugNotify($msg, 5000);
35
        return $this;
36
    }
37
38
    /**
39
     * @param string $msg
40
     * @return $this
41
     */
42
    protected function notifyError($msg)
43
    {
44
        $this->debugNotify($msg, 5000, 'error');
45
        return $this;
46
    }
47
48
    /**
49
     * @param string $msg
50
     * @param int $timeout
51
     * @param string $type
52
     * @return $this
53
     */
54
    protected function debugNotify($msg, $timeout = 2000, $type = 'info')
55
    {
56
        $title = get_class($this) . '::' . $this->getName(false);
57
        $msg   = trim(addslashes(preg_replace('~[[:space:]]+~', ' ', nl2br($msg))));
58
        $args  = '"' . $title . '", "' . $msg . '", ' . $timeout . ', "' . $type . '"';
59
        $cmd   = '("function" === typeof _debugNotify) && _debugNotify(' . $args . ');';
60
61
        $this->getSession()->execute(['script' => $cmd, 'args' => []]);
62
        sleep($timeout / 1000);
63
        return $this;
64
    }
65
}
66