Completed
Push — master ( 45c272...07cbe0 )
by Mr
13s queued 10s
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\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 26
    private function openSocket(): void
38
    {
39 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...
40
41
        // Default: Context for ssl
42 26
        $context = stream_context_create($options);
43
44
        // Default: Proto tcp:// but for ssl we need ssl://
45 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...
46
47
        // Initiate socket client
48 26
        $socket = @stream_socket_client(
49 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...
50 26
            $this->socket_err_num,
51 26
            $this->socket_err_str,
52 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...
53 26
            STREAM_CLIENT_CONNECT,
54 26
            $context
55
        );
56
57
        // Throw error is socket is not initiated
58 26
        if (false === $socket) {
59 1
            throw new ClientException('Unable to establish socket session, ' . $this->socket_err_str);
60
        }
61
62
        //Timeout read
63 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...
64
65
        // Save socket to static variable
66 26
        $this->setSocket($socket);
67 26
    }
68
69
    /**
70
     * Close socket session
71
     *
72
     * @return bool
73
     */
74
    private function closeSocket(): bool
75
    {
76
        return fclose($this->socket);
77
    }
78
79
    /**
80
     * Save socket resource to static variable
81
     *
82
     * @param resource $socket
83
     *
84
     * @return  void
85
     */
86 26
    private function setSocket($socket): void
87
    {
88 26
        $this->socket = $socket;
89 26
    }
90
91
    /**
92
     * Return socket resource if is exist
93
     *
94
     * @return resource
95
     */
96 26
    public function getSocket()
97
    {
98 26
        return $this->socket;
99
    }
100
}
101