Issues (5)

src/Client.php (5 issues)

Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gingdev\SocketIo;
6
7
use Curl\Curl as Socket;
8
use InvalidArgumentException;
9
10
class Client
11
{
12
    /** @var Socket $client */
13
    private $client;
14
15
    /** @var string $host */
16
    private $host;
17
18
    /** @var string $namespace */
19
    private $namespace;
20
21 2
    public function __construct()
22
    {
23
        // Initialize the variable
24 2
        $this->client = new Socket();
25 2
    }
26
27
    /**
28
     * Initialize client
29
     *
30
     * @param string $name
31
     * @param string $token
0 ignored issues
show
Method \Gingdev\SocketIo\Client::initialize() has useless @param annotation for parameter $token.
Loading history...
32
     * @return $this
33
     */
34 2
    public function initialize(string $host, string $token = 'gingdev')
35
    {
36 2
        if (filter_var($host, FILTER_VALIDATE_URL)) {
37 2
            $this->host = $host;
38 2
            $this->client->setHeader('Authorization', 'Bearer ' . $token);
39 2
            $this->namespace = '/';
40
        } else {
41
            throw new InvalidArgumentException('Invalid URL');
42
        }
43 2
        return $this;
44
    }
45
46
    /**
47
     * Change namespace
48
     *
49
     * @param string $namespace
0 ignored issues
show
Method \Gingdev\SocketIo\Client::of() has useless @param annotation for parameter $namespace.
Loading history...
50
     * @return $this
51
     */
52 1
    public function of(string $namespace)
53
    {
54 1
        $this->namespace = $namespace;
55 1
        return $this;
56
    }
57
58
    /**
59
     * Emit data to socket server
60
     *
61
     * @param string $event
0 ignored issues
show
Method \Gingdev\SocketIo\Client::emit() has useless @param annotation for parameter $event.
Loading history...
62
     * @param string|array $data
0 ignored issues
show
@param annotation of method \Gingdev\SocketIo\Client::emit() does not specify type hint for items of its traversable parameter $data.
Loading history...
63
     * @return boolean
0 ignored issues
show
Method \Gingdev\SocketIo\Client::emit() has useless @return annotation.
Loading history...
64
     */
65 2
    public function emit(string $event, $data): bool
66
    {
67
        $args = [
68 2
            'namespace' => $this->namespace,
69 2
            'event'     => $event,
70 2
            'data'      => $data
71
        ];
72 2
        $response = $this->client->post($this->host, $args);
73 2
        if ($response->error) {
74 1
            return false;
75
        }
76 2
        return true;
77
    }
78
79
    /**
80
     * Close client
81
     *
82
     * @return $this
83
     */
84 2
    public function close()
85
    {
86 2
        $this->host = null;
87 2
        $this->client->reset();
88 2
        return $this;
89
    }
90
}
91