Completed
Branch 0.4-dev (79cc15)
by Evgenij
03:32
created

LeEvent::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
namespace AsyncSockets\RequestExecutor\LibEvent;
11
12
use AsyncSockets\RequestExecutor\Metadata\RequestDescriptor;
13
14
/**
15
 * Class LeEvent
16
 */
17
class LeEvent
18
{
19
    /**
20
     * Libevent event handle
21
     *
22
     * @var resource
23
     */
24
    private $handle;
25
26
    /**
27
     * Callback object
28
     *
29
     * @var LeCallbackInterface
30
     */
31
    private $callback;
32
33
    /**
34
     * RequestDescriptor
35
     *
36
     * @var RequestDescriptor
37
     */
38
    private $requestDescriptor;
39
40
    /**
41
     * Timeout for event
42
     *
43
     * @var int|null
44
     */
45
    private $timeout;
46
47
    /**
48
     * LeEvent constructor.
49
     *
50
     * @param LeCallbackInterface $callback Callback object
51
     * @param RequestDescriptor   $requestDescriptor Request descriptor object
52
     * @param int|null            $timeout Timeout for event
53
     */
54
    public function __construct(LeCallbackInterface $callback, RequestDescriptor $requestDescriptor, $timeout)
55
    {
56
        $this->handle            = event_new();
57
        $this->callback          = $callback;
58
        $this->requestDescriptor = $requestDescriptor;
59
        $this->timeout           = $timeout;
60
    }
61
62
    /**
63
     * Destructor
64
     */
65
    public function __destruct()
66
    {
67
        $this->destroy();
68
    }
69
70
    /**
71
     * Return Timeout
72
     *
73
     * @return int|null
74
     */
75
    public function getTimeout()
76
    {
77
        return $this->timeout;
78
    }
79
80
    /**
81
     * Return Handle
82
     *
83
     * @return resource
84
     */
85
    public function getHandle()
86
    {
87
        return $this->handle;
88
    }
89
90
    /**
91
     * Return RequestDescriptor
92
     *
93
     * @return RequestDescriptor
94
     */
95
    public function getRequestDescriptor()
96
    {
97
        return $this->requestDescriptor;
98
    }
99
100
    /**
101
     * Fire event
102
     *
103
     * @param string $eventType Type of event, one of LeCallbackInterface::EVENT_* consts
104
     *
105
     * @return void
106
     */
107
    public function fire($eventType)
108
    {
109
        $this->callback->onEvent($this->requestDescriptor, $eventType);
110
    }
111
112
    /**
113
     * Destroy event handle
114
     *
115
     * @return void
116
     */
117
    private function destroy()
118
    {
119
        if ($this->handle) {
120
            event_del($this->handle);
121
            event_free($this->handle);
122
            $this->handle = null;
123
        }
124
    }
125
}
126