LaraElephantIO::connect()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
namespace Moura137\LaravelElephant;
3
4
use ElephantIO\Client as ElephantIO;
5
6
class LaraElephantIO
7
{
8
    /**
9
     * @var \ElephantIO\Client
10
     */
11
    protected $elephant;
12
13
    /**
14
     * @var boolean
15
     */
16
    protected $init = false;
17
18
    /**
19
     * Construct, initialize o elephant.io
20
     *
21
     * @param ElephantIO $elephant
22
     */
23
    public function __construct(ElephantIO $elephant)
24
    {
25
        $this->elephant = $elephant;
26
    }
27
28
    /**
29
     * Conectar
30
     *
31
     * @return void
32
     */
33
    public function connect()
34
    {
35
        if (!$this->init) {
36
            $this->elephant->initialize();
37
            $this->init = true;
38
        }
39
    }
40
41
    /**
42
     * Emits a message through the engine
43
     *
44
     * @param string $event
45
     * @param array  $args
46
     *
47
     * @return $this
48
     */
49
    public function emit($event, array $args)
50
    {
51
        $this->connect();
52
53
        return $this->elephant->emit($event, $args);
54
    }
55
56
    /**
57
     * Call Methods Elephant
58
     *
59
     * @param string $method
60
     * @param array  $args
61
     */
62
    public function __call($method, $args)
63
    {
64
        $this->connect();
65
66
        return call_user_func_array(array($this->elephant, $method), $args);
67
    }
68
69
    /**
70
     * Destruct, close o elephant.io
71
     *
72
     * @return void
73
     */
74
    public function close()
75
    {
76
        if ($this->init) {
77
            $this->elephant->close();
78
        }
79
80
        $this->init = false;
81
    }
82
83
    /**
84
     * Destruct, close o elephant.io
85
     */
86
    public function __destruct()
87
    {
88
        $this->close();
89
    }
90
}
91