Completed
Push — master ( 10de85...76baec )
by Kacper
03:21
created

PingKeepAlive   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 49
rs 10
c 1
b 0
f 1
wmc 5
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setClient() 0 6 1
A enable() 0 6 1
A disable() 0 4 1
A keepAlive() 0 8 1
1
<?php
2
/**
3
 * Nucleus - XMPP Library for PHP
4
 *
5
 * Copyright (C) 2016, Some rights reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Module;
17
18
19
use Kadet\Xmpp\Stanza\Stanza;
20
use Kadet\Xmpp\Xml\XmlElement;
21
use Kadet\Xmpp\XmppClient;
22
23
class PingKeepAlive extends ClientModule
24
{
25
    private $_interval = 15;
26
    private $_timer;
27
28
    /**
29
     * PingKeepAlive constructor.
30
     *
31
     * @param float $interval Keep alive interval in seconds
32
     */
33
    public function __construct($interval = 15.)
34
    {
35
        $this->_interval = $interval;
0 ignored issues
show
Documentation Bug introduced by
The property $_interval was declared of type integer, but $interval is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
36
    }
37
38
    public function setClient(XmppClient $client)
39
    {
40
        parent::setClient($client);
41
42
        $this->_client->on('state', [$this, 'enable'], \Kadet\Xmpp\Utils\filter\equals('ready'));
43
    }
44
45
    /**
46
     * Starts keep alive timer
47
     */
48
    public function enable()
49
    {
50
        $this->_timer = $this->_client->connector->getLoop()->addPeriodicTimer($this->_interval, function() {
51
            $this->keepAlive();
52
        });
53
    }
54
55
    /**
56
     * Stops keep alive timer
57
     */
58
    public function disable()
59
    {
60
        $this->_client->connector->getLoop()->cancelTimer($this->_timer);
61
    }
62
63
    private function keepAlive()
64
    {
65
        $ping = new Stanza('iq', ['type' => 'get', 'content' => [
66
            new XmlElement('ping', 'urn:xmpp:ping')
67
        ]]);
68
69
        $this->_client->write($ping);
70
    }
71
}
72