Completed
Pull Request — master (#71)
by
unknown
04:48
created

Dripper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 59
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getCheckIntervalAttribute() 0 5 1
A getThresholdAttribute() 0 5 1
A getHtmlAttribute() 0 23 1
A getUrlAttribute() 0 8 1
A getIntervalAttribute() 0 5 1
1
<?php namespace GeneaLabs\LaravelCaffeine;
2
3
use Jenssegers\Model\Model;
4
5
/**
6
 * @property string $html
7
 * @property string $interval
8
 * @property string $url
9
 */
10
class Dripper extends Model
11
{
12
    public function getHtmlAttribute() : string
13
    {
14
        
15
        return '<script>'
16
            . "let ld = new Date();"
17
            . "function caffeineSendDrip () {"
18
            . "    let e = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');"
19
            . "    e.onreadystatechange = function () {"
20
            . "        if (e.readyState === 4 && e.status === 204) {"
21
            . "            ld = new Date();"
22
            . "        }"
23
            . "    };"
24
            . "    e.open('GET', '{$this->url}', !0);"
25
            . "    e.setRequestHeader('X-Requested-With', 'XMLHttpRequest');"
26
            . "    e.send();"
27
            . "}"
28
            . "setInterval(function () { caffeineSendDrip(); }, $this->interval);"
29
            . "setInterval(function () {"
30
            . "    if (new Date() - ld >= $this->interval + $this->threshold) {"
1 ignored issue
show
Bug Best Practice introduced by
The property threshold does not exist on GeneaLabs\LaravelCaffeine\Dripper. Since you implemented __get, consider adding a @property annotation.
Loading history...
31
            . "        location.reload(true);"
32
            . "    }"
33
            . "}, $this->checkInterval);"
1 ignored issue
show
Bug Best Practice introduced by
The property checkInterval does not exist on GeneaLabs\LaravelCaffeine\Dripper. Since you implemented __get, consider adding a @property annotation.
Loading history...
34
            . "</script>";
35
    }
36
37
    public function getIntervalAttribute() : string
38
    {
39
        return config(
40
            'genealabs-laravel-caffeine.dripIntervalInMilliSeconds',
41
            300000
42
        );
43
    }
44
    
45
    public function getThresholdAttribute() : int
46
    {
47
        return config(
48
            'genealabs-laravel-caffeine.thresholdDifference',
49
            10000
50
        );
51
    }
52
    
53
    public function getCheckIntervalAttribute() : int
54
    {
55
        return config(
56
            'genealabs-laravel-caffeine.checkLastDripInterval',
57
            2000
58
        );
59
    }
60
61
    public function getUrlAttribute() : string
62
    {
63
        return trim(config('genealabs-laravel-caffeine.domain', url('/')), '/')
64
            . '/'
65
            . trim(config(
66
                'genealabs-laravel-caffeine.route',
67
                'genealabs/laravel-caffeine/drip'
68
            ), '/');
69
    }
70
}
71