Passed
Push — master ( a2028d...6ece19 )
by Benjamin
01:48
created

TcpTransport::setMessageEncoder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
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\Encoder\EncoderInterface;
15
use Gelf\Encoder\NoNullByteEncoderInterface;
16
use Gelf\MessageInterface as Message;
17
use Gelf\Encoder\JsonEncoder as DefaultEncoder;
18
use InvalidArgumentException;
19
20
/**
21
 * TcpTransport allows the transfer of GELF-messages (with SSL/TLS support)
22
 * to a compatible GELF-TCP-backend as described in
23
 * https://github.com/Graylog2/graylog2-docs/wiki/GELF
24
 *
25
 * It can also act as a direct publisher
26
 *
27
 * @author Benjamin Zikarsky <[email protected]>
28
 * @author Ahmed Trabelsi <[email protected]>
29
 */
30
class TcpTransport extends AbstractTransport
31
{
32
    const DEFAULT_HOST = "127.0.0.1";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal 127.0.0.1 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
33
    const DEFAULT_PORT = 12201;
34
35
    const AUTO_SSL_PORT = 12202;
36
37
    /**
38
     * @var string
39
     */
40
    protected $host;
41
42
    /**
43
     * @var int
44
     */
45
    protected $port;
46
47
    /**
48
     * @var StreamSocketClient
49
     */
50
    protected $socketClient;
51
52
    /**
53
     * @var SslOptions|null
54
     */
55
    protected $sslOptions = null;
56
57
    /**
58
     * Class constructor
59
     *
60
     * @param string|null     $host       when NULL or empty default-host is used
61
     * @param int|null        $port       when NULL or empty default-port is used
62
     * @param SslOptions|null $sslOptions when null not SSL is used
63
     */
64 8
    public function __construct(
65
        $host = self::DEFAULT_HOST,
66
        $port = self::DEFAULT_PORT,
67
        SslOptions $sslOptions = null
68
    ) {
69 8
        $this->host = $host;
70 8
        $this->port = $port;
71
72 8
        if ($port == self::AUTO_SSL_PORT && $sslOptions == null) {
73 1
            $sslOptions = new SslOptions();
74
        }
75
76 8
        $this->sslOptions = $sslOptions;
77
78 8
        $this->setMessageEncoder(new DefaultEncoder());
79 8
        $this->socketClient = new StreamSocketClient(
80 8
            $this->getScheme(),
81 8
            $this->host,
82 8
            $this->port,
83 8
            $this->getContext()
84
        );
85 8
    }
86
87
    /**
88
     * Sends a Message over this transport
89
     *
90
     * @param Message $message
91
     *
92
     * @return int the number of TCP packets sent
93
     */
94 1
    public function send(Message $message)
95
    {
96 1
        $rawMessage = $this->getMessageEncoder()->encode($message) . "\0";
97
98
        // send message in one packet
99 1
        $this->socketClient->write($rawMessage);
100
101 1
        return 1;
102
    }
103
104
    /**
105
     * @return string
106
     */
107 8
    private function getScheme()
108
    {
109 8
        return null === $this->sslOptions ? 'tcp' : 'ssl';
110
    }
111
112
    /**
113
     * @return array
114
     */
115 8
    private function getContext()
116
    {
117 8
        if (null === $this->sslOptions) {
118 8
            return array();
119
        }
120
121 2
        return $this->sslOptions->toStreamContext($this->host);
122
    }
123
124
    /**
125
     * Sets the connect-timeout
126
     *
127
     * @param int $timeout
128
     */
129 1
    public function setConnectTimeout($timeout)
130
    {
131 1
        $this->socketClient->setConnectTimeout($timeout);
132 1
    }
133
134
    /**
135
     * Returns the connect-timeout
136
     *
137
     * @return int
138
     */
139 1
    public function getConnectTimeout()
140
    {
141 1
        return $this->socketClient->getConnectTimeout();
142
    }
143
144 8
    public function setMessageEncoder(EncoderInterface $encoder)
145
    {
146 8
        if (!$encoder instanceof NoNullByteEncoderInterface) {
147 1
            throw new InvalidArgumentException(
148 1
                "TcpTransport only works with NoNullByteEncoderInterface encoders"
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal TcpTransport only works ...coderInterface encoders does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
149
            );
150
        }
151
152 8
        return parent::setMessageEncoder($encoder);
153
    }
154
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
155