Completed
Push — master ( 93f92b...059c3f )
by Mr
03:57
created

SocketTrait::setSocket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace RouterOS;
4
5
use RouterOS\Exceptions\ClientException;
6
7
trait SocketTrait
8
{
9
    /**
10
     * Initiate socket session
11
     *
12
     * @return  void
13
     * @throws  \RouterOS\Exceptions\ClientException
14
     * @throws  \RouterOS\Exceptions\ConfigException
15
     */
16 7
    private function openSocket()
17
    {
18
        // Default: Context for ssl
19 7
        $context = stream_context_create([
20 7
            'ssl' => [
21
                'ciphers'          => 'ADH:ALL',
22
                'verify_peer'      => false,
23
                'verify_peer_name' => false
24
            ]
25
        ]);
26
27
        // Default: Proto tcp:// but for ssl we need ssl://
28 7
        $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...
29
30
        // Initiate socket client
31 7
        $socket = @stream_socket_client(
32 7
            $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...
33 7
            $this->_socket_err_num,
0 ignored issues
show
Bug introduced by
The property _socket_err_num does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34 7
            $this->_socket_err_str,
0 ignored issues
show
Bug introduced by
The property _socket_err_str does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35 7
            $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...
36 7
            STREAM_CLIENT_CONNECT,
37 7
            $context
38
        );
39
40
        // Throw error is socket is not initiated
41 7
        if (!$socket) {
42 1
            throw new ClientException('Unable to establish socket session, ' . $this->_socket_err_str);
43
        }
44
45
        // Save socket to static variable
46 6
        $this->setSocket($socket);
47 6
    }
48
49
    /**
50
     * Close socket session
51
     *
52
     * @return bool
53
     */
54 1
    private function closeSocket(): bool
55
    {
56 1
        return fclose($this->_socket);
0 ignored issues
show
Bug introduced by
The property _socket does not seem to exist. Did you mean _socket_err_num?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
57
    }
58
59
    /**
60
     * Save socket resource to static variable
61
     *
62
     * @param   resource $socket
63
     * @return  void
64
     */
65 6
    private function setSocket($socket)
66
    {
67 6
        $this->_socket = $socket;
0 ignored issues
show
Bug introduced by
The property _socket does not seem to exist. Did you mean _socket_err_num?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
68 6
    }
69
70
    /**
71
     * Return socket resource if is exist
72
     *
73
     * @return  resource
74
     */
75 6
    public function getSocket()
76
    {
77 6
        return $this->_socket;
0 ignored issues
show
Bug introduced by
The property _socket does not seem to exist. Did you mean _socket_err_num?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
78
    }
79
}
80