Passed
Push — develop ( 065f10...1d4e29 )
by Aleksandr
01:25
created

ClientBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlexCool\Rcon\Client\Minecraft;
4
5
use Swoole\Client as SwooleClient;
6
use AlexCool\Rcon\Configurator\BuilderConfigurator;
7
8
/**
9
 * @author Aleksandr Kulina <[email protected]>
10
 *
11
 * @package AlexCool\Rcon\Client
12
 */
13
final class ClientBuilder
14
{
15
    /**
16
     * Swoole client constants
17
     */
18
    const OPEN_LENGTH_CHECK = true;
19
    const PACKAGE_LENGTH_TYPE = 'V';
20
    const PACKAGE_LENGTH_OFFSET = 0; // The offset of package length variable
21
    const PACKAGE_BODY_OFFSET = 4; // The offset of body of the package
22
23
    /**
24
     * @var SwooleClient
25
     */
26
    private $swooleClient;
27
28
    /**
29
     * @var BuilderConfigurator
30
     */
31
    private $config;
32
33
    /**
34
     * @param BuilderConfigurator $config
35
     */
36
    public function __construct(BuilderConfigurator $config)
37
    {
38
        $this->config = $config;
39
    }
40
41
    /**
42
     * @return $this
43
     */
44
    public function createSwooleClient()
45
    {
46
        $this->swooleClient = new SwooleClient(SWOOLE_SOCK_TCP);
47
        $this->swooleClient->set([
48
            'open_length_check' => self::OPEN_LENGTH_CHECK,
49
            'package_length_type' => self::PACKAGE_LENGTH_TYPE,
50
            'package_length_offset' => self::PACKAGE_LENGTH_OFFSET,
51
            'package_body_offset' => self::PACKAGE_BODY_OFFSET,
52
        ]);
53
54
        return $this;
55
    }
56
57
    /**
58
     * @return Client
59
     */
60
    public function getClient()
61
    {
62
        return new Client(
63
            $this->swooleClient,
64
            $this->config->getHost(),
65
            $this->config->getRconPort(),
66
            $this->config->getRconPassword(),
67
            $this->config->getRconTimeout()
68
        );
69
    }
70
}
71