TcpTransport::setMessageEncoder()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
ccs 3
cts 3
cp 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * This file is part of the php-gelf package.
6
 *
7
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Gelf\Transport;
14
15
use Gelf\Encoder\EncoderInterface;
16
use Gelf\Encoder\NoNullByteEncoderInterface;
17
use Gelf\MessageInterface as Message;
18
use Gelf\Encoder\JsonEncoder as DefaultEncoder;
19
use InvalidArgumentException;
20
21
/**
22
 * TcpTransport allows the transfer of GELF-messages (with SSL/TLS support)
23
 * to a compatible GELF-TCP-backend as described in
24
 * https://github.com/Graylog2/graylog2-docs/wiki/GELF
25
 *
26
 * It can also act as a direct publisher
27
 *
28
 * @author Benjamin Zikarsky <[email protected]>
29
 * @author Ahmed Trabelsi <[email protected]>
30
 */
31
class TcpTransport extends AbstractTransport
32
{
33
    private const DEFAULT_HOST = "127.0.0.1";
34
    private const DEFAULT_PORT = 12201;
35
    private const AUTO_SSL_PORT = 12202;
36
37
    private StreamSocketClient $socketClient;
38
39
    public function __construct(
40
        private string $host = self::DEFAULT_HOST,
41
        private int $port = self::DEFAULT_PORT,
42
        private ?SslOptions $sslOptions = null
43
    ) {
44
        parent::__construct();
45
46
        if ($port == self::AUTO_SSL_PORT && $this->sslOptions == null) {
47
            $this->sslOptions = new SslOptions();
48
        }
49
50
        $this->socketClient = new StreamSocketClient(
51
            $this->getScheme(),
52
            $this->host,
53
            $this->port,
54
            $this->getContext()
55
        );
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function send(Message $message): int
62
    {
63
        $rawMessage = $this->getMessageEncoder()->encode($message) . "\0";
64 8
65
        // send message in one packet
66
        return $this->socketClient->write($rawMessage);
67
    }
68
69 8
    private function getScheme(): string
70 8
    {
71
        return null === $this->sslOptions ? 'tcp' : 'ssl';
72 8
    }
73 1
74
    private function getContext(): array
75
    {
76 8
        if (null === $this->sslOptions) {
77
            return [];
78 8
        }
79 8
80 8
        return $this->sslOptions->toStreamContext($this->host);
81 8
    }
82 8
83 8
    /**
84
     * Sets the connect-timeout
85 8
     */
86
    public function setConnectTimeout(int $timeout): void
87
    {
88
        $this->socketClient->setConnectTimeout($timeout);
89
    }
90
91
    /**
92
     * Returns the connect-timeout
93
     */
94 1
    public function getConnectTimeout(): int
95
    {
96 1
        return $this->socketClient->getConnectTimeout();
97
    }
98
99 1
    public function setMessageEncoder(EncoderInterface $encoder): static
100
    {
101 1
        if (!$encoder instanceof NoNullByteEncoderInterface) {
102
            throw new InvalidArgumentException(
103
                "TcpTransport only works with NoNullByteEncoderInterface encoders"
104
            );
105
        }
106
107 8
        parent::setMessageEncoder($encoder);
108
        return $this;
109 8
    }
110
}
111