ASocket::__destruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
namespace kalanis\RemoteRequest\Sockets;
4
5
6
use kalanis\RemoteRequest\Interfaces\IConnectionParams;
7
use kalanis\RemoteRequest\Interfaces\IRRTranslations;
8
use kalanis\RemoteRequest\RequestException;
9
use kalanis\RemoteRequest\Traits\TLang;
10
11
12
/**
13
 * Class ASocket
14
 * @package kalanis\RemoteRequest\Sockets
15
 * Network sockets to the remote server - base abstract method
16
 */
17
abstract class ASocket
18
{
19
    use TLang;
20
21
    /** @var resource|null */
22
    protected $pointer = null;
23
24 57
    public function __construct(?IRRTranslations $lang = null)
25
    {
26 57
        $this->setRRLang($lang);
27
    }
28
29 57
    public function __destruct()
30
    {
31 57
        $this->close();
32
    }
33
34 57
    public function close(): void
35
    {
36 57
        if (!empty($this->pointer)) {
37 5
            fclose($this->pointer);
38 5
            $this->pointer = null;
39
        }
40
    }
41
42
    /**
43
     * @param IConnectionParams $params
44
     * @throws RequestException
45
     * @return resource
46
     */
47
    abstract protected function remotePointer(IConnectionParams $params);
48
49
    /**
50
     * @param IConnectionParams $params
51
     * @throws RequestException
52
     * @return resource|null
53
     */
54 7
    public function getResourcePointer(IConnectionParams $params)
55
    {
56 7
        if (empty($this->pointer)) {
57 7
            $this->pointer = $this->remotePointer($params);
58
        } else {
59 1
            rewind($this->pointer);
60
        }
61 6
        return $this->pointer;
62
    }
63
}
64