Completed
Push — master ( ae114e...0e8cdf )
by Mr
14:47 queued 07:50
created

SocketTrait::closeSocket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 0
crap 2
1
<?php
2
3
namespace RouterOS;
4
5
use RouterOS\Exceptions\ClientException;
6
7
trait SocketTrait
8
{
9
    /**
10
     * Socket resource
11
     *
12
     * @var resource|null
13
     */
14
    private $_socket;
15
16
    /**
17
     * Code of error
18
     *
19
     * @var int
20
     */
21
    private $_socket_err_num;
22
23
    /**
24
     * Description of socket error
25
     *
26
     * @var string
27
     */
28
    private $_socket_err_str;
29
30
    /**
31
     * Initiate socket session
32
     *
33
     * @return  void
34
     * @throws  \RouterOS\Exceptions\ClientException
35
     * @throws  \RouterOS\Exceptions\ConfigException
36
     */
37 16
    private function openSocket()
38
    {
39
        // Default: Context for ssl
40 16
        $context = stream_context_create([
41
            'ssl' => [
42 16
                'ciphers'          => 'ADH:ALL',
43
                'verify_peer'      => false,
44
                'verify_peer_name' => false
45
            ]
46
        ]);
47
48
        // Default: Proto tcp:// but for ssl we need ssl://
49 16
        $proto = $this->config('ssl') ? 'ssl://' : '';
0 ignored issues
show
Bug introduced by
It seems like config() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
50
51
        // Initiate socket client
52 16
        $socket = @stream_socket_client(
53 16
            $proto . $this->config('host') . ':' . $this->config('port'),
0 ignored issues
show
Bug introduced by
It seems like config() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54 16
            $this->_socket_err_num,
55 16
            $this->_socket_err_str,
56 16
            $this->config('timeout'),
0 ignored issues
show
Bug introduced by
It seems like config() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57 16
            STREAM_CLIENT_CONNECT,
58
            $context
59
        );
60
61
        // Throw error is socket is not initiated
62 16
        if (false === $socket) {
63 1
            throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str);
64
        }
65
66
        // Save socket to static variable
67 15
        $this->setSocket($socket);
68 15
    }
69
70
    /**
71
     * Close socket session
72
     *
73
     * @return bool
74
     */
75
    private function closeSocket(): bool
76
    {
77
        return fclose($this->_socket);
78
    }
79
80
    /**
81
     * Save socket resource to static variable
82
     *
83
     * @param   resource $socket
84
     * @return  void
85
     */
86 15
    private function setSocket($socket)
87
    {
88 15
        $this->_socket = $socket;
89 15
    }
90
91
    /**
92
     * Return socket resource if is exist
93
     *
94
     * @return  resource
95
     */
96 15
    public function getSocket()
97
    {
98 15
        return $this->_socket;
99
    }
100
}
101