Passed
Push — master ( b4aac6...30a31a )
by Thanh
01:58
created

Client::of()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gingdev\SocketIo;
6
7
use Curl\Curl;
8
use InvalidArgumentException;
9
10
class Client
11
{
12
    /** @var Curl $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 Curl();
25 2
    }
26
27
    /**
28
     * Initialize client
29
     *
30
     * @param string $host
0 ignored issues
show
introduced by
Method \Gingdev\SocketIo\Client::initialize() has useless @param annotation for parameter $host.
Loading history...
31
     * @param string $token
0 ignored issues
show
introduced by
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
            throw new InvalidArgumentException('The host name is not valid');
38
        }
39 2
        $this->host = rtrim($host, '/');
40 2
        $this->client->setHeader('Authorization', 'Bearer ' . $token);
41 2
        return $this;
42
    }
43
    
44
    /**
45
     * Change namespace
46
     * 
47
     * @param string $namespace
0 ignored issues
show
introduced by
Method \Gingdev\SocketIo\Client::of() has useless @param annotation for parameter $namespace.
Loading history...
48
     * @return $this
49
     */
50 1
    public function of(string $namespace)
51
    {
52 1
        $this->namespace = $namespace;
53 1
        return $this;
54
    }
55
    
56
    /**
57
     * Emit data to socket server
58
     *
59
     * @param string $event
0 ignored issues
show
introduced by
Method \Gingdev\SocketIo\Client::emit() has useless @param annotation for parameter $event.
Loading history...
60
     * @param array $data
0 ignored issues
show
introduced by
@param annotation of method \Gingdev\SocketIo\Client::emit() does not specify type hint for items of its traversable parameter $data.
Loading history...
61
     * @return boolean
0 ignored issues
show
introduced by
Method \Gingdev\SocketIo\Client::emit() has useless @return annotation.
Loading history...
62
     */
63 2
    public function emit(string $event, array $data = []): bool
64
    {
65
        $args = [
66 2
            'namespace'    => $this->namespace,
67 2
            'event'        => $event,
68 2
            'data'         => $data
69
        ];
70 2
        $response = $this->client->post($this->host . '/api', $args);
71 2
        if ($response->error) {
72
            return false;
73
        }
74 2
        return true;
75
    }
76
77
    /**
78
     * Close client
79
     *
80
     * @return $this
81
     */
82
    public function close()
83
    {
84
        $this->client->reset();
85
        return $this;
86
    }
87
}
88