Completed
Pull Request — master (#101)
by
unknown
08:31
created

TcpTransport::getContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * This file is part of the php-gelf package.
5
 *
6
 * (c) Benjamin Zikarsky <http://benjamin-zikarsky.de>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gelf\Transport;
13
14
use Gelf\MessageInterface as Message;
15
use Gelf\Encoder\JsonEncoder as DefaultEncoder;
16
17
/**
18
 * TcpTransport allows the transfer of GELF-messages (with SSL/TLS support)
19
 * to a compatible GELF-TCP-backend as described in
20
 * https://github.com/Graylog2/graylog2-docs/wiki/GELF
21
 *
22
 * It can also act as a direct publisher
23
 *
24
 * @author Benjamin Zikarsky <[email protected]>
25
 * @author Ahmed Trabelsi <[email protected]>
26
 */
27
class TcpTransport extends AbstractTransport
28
{
29
    /**
30
     * @var string
31
     */
32
    protected $host = "127.0.0.1";
33
34
    /**
35
     * @var int
36
     */
37
    protected $port = 12201;
38
39
    /**
40
     * @var StreamSocketClient
41
     */
42
    protected $socketClient;
43
44 4
    /**
45
     * @var SslOptions|null
46
     */
47 4
    protected $sslOptions = null;
48 4
49
    /**
50 4
     * Class constructor
51 4
     *
52 4
     * @param string|null     $host       when NULL or empty default-host is used
53
     * @param int|null        $port       when NULL or empty default-port is used
54
     * @param SslOptions|null $sslOptions when null not SSL is used
55
     */
56
    public function __construct($host = null, $port = null, SslOptions $sslOptions = null)
57
    {
58
        // allow NULL-like values for fallback on default
59
        $this->host = $host ?: $this->host;
60
        $this->port = $port ?: $this->port;
61 1
62
        if ($port == 12202 && $sslOptions == null) {
63 1
            $sslOptions = new SslOptions();
64
        }
65
66 1
        $this->sslOptions = $sslOptions;
67
68 1
        $this->messageEncoder = new DefaultEncoder();
69
        $this->socketClient = new StreamSocketClient(
70
            $this->getScheme(),
71
            $this->host,
72
            $this->port,
73
            $this->getContext()
74
        );
75
    }
76 1
77
    /**
78 1
     * Sends a Message over this transport
79 1
     *
80
     * @param Message $message
81
     *
82
     * @return int the number of TCP packets sent
83
     */
84
    public function send(Message $message)
85
    {
86 1
        $rawMessage = $this->getMessageEncoder()->encode($message) . "\0";
87
88 1
        // send message in one packet
89
        $this->socketClient->write($rawMessage);
90
91
        return 1;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    private function getScheme()
98
    {
99
        return null === $this->sslOptions ? 'tcp' : 'ssl';
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    private function getContext()
106
    {
107
        if (null === $this->sslOptions) {
108
            return array();
109
        }
110
111
        return $this->sslOptions->toStreamContext($this->host);
112
    }
113
114
    /**
115
     * Sets the connect-timeout
116
     *
117
     * @param int $timeout
118
     */
119
    public function setConnectTimeout($timeout)
120
    {
121
        $this->socketClient->setConnectTimeout($timeout);
122
    }
123
124
    /**
125
     * Returns the connect-timeout
126
     *
127
     * @return int
128
     */
129
    public function getConnectTimeout()
130
    {
131
        return $this->socketClient->getConnectTimeout();
132
    }
133
}
134