Passed
Push — master ( 47f096...b4aac6 )
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 0
CRAP Score 2

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 0
cts 3
cp 0
crap 2
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 1
    public function __construct()
22
    {
23
        // Initialize the variable
24 1
        $this->client = new Curl();
25 1
    }
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 1
    public function initialize(string $host, string $token = 'gingdev')
35
    {
36 1
        if (! filter_var($host, FILTER_VALIDATE_URL)) {
37
            throw new InvalidArgumentException('The host name is not valid');
38
        }
39 1
        $this->host = rtrim($host, '/');
40 1
        $this->client->setHeader('Authorization', 'Bearer ' . $token);
41 1
        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
    public function of(string $namespace)
51
    {
52
        $this->namespace = $namespace;
53
        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 1
    public function emit(string $event, array $data = []): bool
64
    {
65
        $args = [
66 1
            'namespace'    => $this->namespace,
67 1
            'event'        => $event,
68 1
            'data'         => $data
69
        ];
70 1
        $response = $this->client->post($this->host . '/api', $args);
71 1
        if ($response->error) {
72
            return false;
73
        }
74 1
        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