DevModeNotifier::onEverySecond()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 16
cp 0
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
4
namespace eXpansion\Framework\Core\Plugins;
5
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
6
use eXpansion\Framework\Core\Helpers\ChatNotification;
7
use eXpansion\Framework\Core\Services\Console;
8
9
10
/**
11
 * Class DevModeNotifier
12
 *
13
 * @package eXpansion\Framework\Core\Plugins;
14
 * @author  oliver de Cramer <[email protected]>
15
 */
16
class DevModeNotifier implements ListenerInterfaceExpTimer
17
{
18
    /** @var ChatNotification */
19
    protected $chatNotification;
20
21
    /** @var Console */
22
    protected $console;
23
24
    /** @var int How often notificaiton needs to be displayed */
25
    protected $interval = 300;
26
27
    /** @var int last time it was displayed. */
28
    protected $lastDisplayTime;
29
30
    /**
31
     * DevModeNotifier constructor.
32
     *
33
     * @param ChatNotification $chatNotification
34
     * @param Console         $console
35
     */
36
    public function __construct(ChatNotification $chatNotification, Console $console)
37
    {
38
        $this->chatNotification = $chatNotification;
39
        $this->console = $console;
40
41
        // Display once 30 seconds after start.
42
        $this->lastDisplayTime = time() - $this->interval + 10;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function onPreLoop()
49
    {
50
        // nothing
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function onPostLoop()
57
    {
58
        // nothing
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function onEverySecond()
65
    {
66
        if (time() > $this->lastDisplayTime + $this->interval) {
67
            $this->lastDisplayTime = time();
68
69
            $this->console->getSfStyleOutput()->warning(
70
                [
71
                    "!! eXpansion is running in dev mode !!",
72
                    "In dev mode eXpansion is not stable and will leak memory which will cause crash.",
73
                    "This is normal behaviour. Please use prod mode. ",
74
                    "",
75
                    "If you are currently developing please ignore this message.",
76
                ]
77
            );
78
            $this->chatNotification->sendMessage("{warning} eXpansion is in dev mode. Memory leaks are normal.");
79
        }
80
    }
81
}