Completed
Push — master ( 575f3e...15d27a )
by Mr
03:47
created

SocketTrait::getSocket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 16
            'ssl' => [
42
                '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 16
            $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
        //Timeout read
67 15
        stream_set_timeout($socket, $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...
68
        
69
        // Save socket to static variable
70 15
        $this->setSocket($socket);
71 15
    }
72
73
    /**
74
     * Close socket session
75
     *
76
     * @return bool
77
     */
78
    private function closeSocket(): bool
79
    {
80
        return fclose($this->_socket);
81
    }
82
83
    /**
84
     * Save socket resource to static variable
85
     *
86
     * @param   resource $socket
87
     * @return  void
88
     */
89 15
    private function setSocket($socket)
90
    {
91 15
        $this->_socket = $socket;
92 15
    }
93
94
    /**
95
     * Return socket resource if is exist
96
     *
97
     * @return  resource
98
     */
99 15
    public function getSocket()
100
    {
101 15
        return $this->_socket;
102
    }
103
}
104