Completed
Push — master ( dd6878...ac34b2 )
by Mr
09:13
created

src/SocketTrait.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace RouterOS;
4
5
use RouterOS\Exceptions\ConnectException;
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\ConnectException
36
     * @throws \RouterOS\Exceptions\ConfigException
37
     */
38 26
    private function openSocket(): void
39
    {
40 26
        $options = ['ssl' => $this->config('ssl_options')];
0 ignored issues
show
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...
41
42
        // Default: Context for ssl
43 26
        $context = stream_context_create($options);
44
45
        // Default: Proto tcp:// but for ssl we need ssl://
46 26
        $proto = $this->config('ssl') ? 'ssl://' : '';
0 ignored issues
show
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...
47
48
        // Initiate socket client
49 26
        $socket = @stream_socket_client(
50 26
            $proto . $this->config('host') . ':' . $this->config('port'),
0 ignored issues
show
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...
51 26
            $this->socket_err_num,
52 26
            $this->socket_err_str,
53 26
            $this->config('timeout'),
0 ignored issues
show
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 26
            STREAM_CLIENT_CONNECT,
55 26
            $context
56
        );
57
58
        // Throw error is socket is not initiated
59 26
        if (false === $socket) {
60 1
            throw new ConnectException('Unable to establish socket session, ' . $this->socket_err_str);
61
        }
62
63
        //Timeout read
64 26
        stream_set_timeout($socket, $this->config('timeout'));
0 ignored issues
show
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...
65
66
        // Save socket to static variable
67 26
        $this->setSocket($socket);
68 26
    }
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
     *
85
     * @return  void
86
     */
87 26
    private function setSocket($socket): void
88
    {
89 26
        $this->socket = $socket;
90 26
    }
91
92
    /**
93
     * Return socket resource if is exist
94
     *
95
     * @return resource
96
     */
97 26
    public function getSocket()
98
    {
99 26
        return $this->socket;
100
    }
101
}
102