Completed
Push — master ( 8fa389...fb04ce )
by De Cramer
10s
created

DatabaseConnectionPersist::onPreLoop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Plugins;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
6
use Propel\Runtime\Propel;
7
use Psr\Log\LoggerInterface;
8
9
/**
10
 * Class DatabaseConnectionPersist
11
 *
12
 * @author    de Cramer Oliver<[email protected]>
13
 * @copyright 2018 Smile
14
 * @package eXpansion\Framework\Core\Plugins
15
 */
16
class DatabaseConnectionPersist implements ListenerInterfaceExpTimer
17
{
18
    /** @var int */
19
    protected $lastPing = 0;
20
21
    /** @var int */
22
    protected $pingInterval;
23
24
    /** @var LoggerInterface */
25
    protected $logger;
26
27
    /**
28
     * DatabaseConnectionPersist constructor.
29
     *
30
     * @param int $pingInterval
31
     * @param LoggerInterface $logger
32
     */
33
    public function __construct(int $pingInterval, LoggerInterface $logger)
34
    {
35
        $this->pingInterval = $pingInterval;
36
        $this->logger = $logger;
37
    }
38
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function onPreLoop()
44
    {
45
        // Nothing.
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function onPostLoop()
52
    {
53
        // Nothing.
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function onEverySecond()
60
    {
61
        if ((time() - $this->lastPing) > $this->pingInterval) {
62
            Propel::getConnection()->inTransaction();
63
64
            $this->logger->debug('Pinged database to persist connection!');
65
            $this->lastPing = time();
66
        }
67
    }
68
}