Passed
Push — master ( 4a8a13...577366 )
by Thanh
04:12 queued 02:27
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 as Socket;
8
9
class Client
10
{
11
    const HEROKU_PLATFORM = 'herokuapp.com';
0 ignored issues
show
Coding Style introduced by
Visibility must be declared on all constants if your project supports PHP 7.1 or later
Loading history...
12
    
13
    const GLITCH_PLATFORM = 'glitch.me';
0 ignored issues
show
Coding Style introduced by
Visibility must be declared on all constants if your project supports PHP 7.1 or later
Loading history...
14
    
15
    /** @var Socket $client */
16
    private $client;
17
18
    /** @var string $host */
19
    private $host;
20
    
21
    /** @var string $namespace */
22
    private $namespace;
23
24 2
    public function __construct()
25
    {
26
        // Initialize the variable
27 2
        $this->client = new Socket();
28 2
    }
29
30
    /**
31
     * Initialize client
32
     *
33
     * @param string $name
0 ignored issues
show
introduced by
Method \Gingdev\SocketIo\Client::initialize() has useless @param annotation for parameter $name.
Loading history...
34
     * @param string $token
0 ignored issues
show
introduced by
Method \Gingdev\SocketIo\Client::initialize() has useless @param annotation for parameter $token.
Loading history...
35
     * @param string $platform
0 ignored issues
show
introduced by
Method \Gingdev\SocketIo\Client::initialize() has useless @param annotation for parameter $platform.
Loading history...
36
     * @return $this
37
     */
38 2
    public function initialize(
39
        string $name,
40
        string $token = 'gingdev',
41
        string $platform = self::HEROKU_PLATFORM
42
    )
43
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
44 2
        $this->host = sprintf("https://%s.%s", $name, $platform);
45 2
        $this->client->setHeader('Authorization', 'Bearer ' . $token);
46 2
        $this->namespace = '/';
47 2
        return $this;
48
    }
49
    
50
    /**
51
     * Change namespace
52
     * 
53
     * @param string $namespace
0 ignored issues
show
introduced by
Method \Gingdev\SocketIo\Client::of() has useless @param annotation for parameter $namespace.
Loading history...
54
     * @return $this
55
     */
56 1
    public function of(string $namespace)
57
    {
58 1
        $this->namespace = $namespace;
59 1
        return $this;
60
    }
61
    
62
    /**
63
     * Emit data to socket server
64
     *
65
     * @param string $event
0 ignored issues
show
introduced by
Method \Gingdev\SocketIo\Client::emit() has useless @param annotation for parameter $event.
Loading history...
66
     * @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...
67
     * @return boolean
0 ignored issues
show
introduced by
Method \Gingdev\SocketIo\Client::emit() has useless @return annotation.
Loading history...
68
     */
69 2
    public function emit(string $event, array $data = []): bool
70
    {
71
        $args = [
72 2
            'namespace' => $this->namespace,
73 2
            'event'     => $event,
74 2
            'data'      => $data
75
        ];
76 2
        $response = $this->client->post($this->host . '/api', $args);
77 2
        if ($response->error) {
78
            return false;
79
        }
80 2
        return true;
81
    }
82
83
    /**
84
     * Close client
85
     *
86
     * @return $this
87
     */
88 2
    public function close()
89
    {
90 2
        $this->client->reset();
91 2
        return $this;
92
    }
93
}
94