Passed
Push — master ( 86e322...9ca9c4 )
by Benjamin
09:12 queued 07:15
created

TcpTransport::getScheme()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
    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...
30
    const DEFAULT_PORT = 12201;
31
32
    const AUTO_SSL_PORT = 12202;
33
34
    /**
35
     * @var string
36
     */
37
    protected $host;
38
39
    /**
40
     * @var int
41
     */
42
    protected $port;
43
44
    /**
45
     * @var StreamSocketClient
46
     */
47
    protected $socketClient;
48
49
    /**
50
     * @var SslOptions|null
51
     */
52
    protected $sslOptions = null;
53
54
    /**
55
     * Class constructor
56
     *
57
     * @param string|null     $host       when NULL or empty default-host is used
58
     * @param int|null        $port       when NULL or empty default-port is used
59
     * @param SslOptions|null $sslOptions when null not SSL is used
60
     */
61 6
    public function __construct(
62
        $host = self::DEFAULT_HOST,
63
        $port = self::DEFAULT_PORT,
64
        SslOptions $sslOptions = null
65
    ) {
66 6
        $this->host = $host;
67 6
        $this->port = $port;
68
69 6
        if ($port == self::AUTO_SSL_PORT && $sslOptions == null) {
70 1
            $sslOptions = new SslOptions();
71 1
        }
72
73 6
        $this->sslOptions = $sslOptions;
74
75 6
        $this->messageEncoder = new DefaultEncoder();
76 6
        $this->socketClient = new StreamSocketClient(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
77 6
            $this->getScheme(),
78 6
            $this->host,
79 6
            $this->port,
80 6
            $this->getContext()
81 6
        );
82 6
    }
83
84
    /**
85
     * Sends a Message over this transport
86
     *
87
     * @param Message $message
88
     *
89
     * @return int the number of TCP packets sent
90
     */
91 1
    public function send(Message $message)
92
    {
93 1
        $rawMessage = $this->getMessageEncoder()->encode($message) . "\0";
94
95
        // send message in one packet
96 1
        $this->socketClient->write($rawMessage);
97
98 1
        return 1;
99
    }
100
101
    /**
102
     * @return string
103
     */
104 6
    private function getScheme()
105
    {
106 6
        return null === $this->sslOptions ? 'tcp' : 'ssl';
107
    }
108
109
    /**
110
     * @return array
111
     */
112 6
    private function getContext()
113
    {
114 6
        if (null === $this->sslOptions) {
115 6
            return array();
116
        }
117
118 2
        return $this->sslOptions->toStreamContext($this->host);
119
    }
120
121
    /**
122
     * Sets the connect-timeout
123
     *
124
     * @param int $timeout
125
     */
126 1
    public function setConnectTimeout($timeout)
127
    {
128 1
        $this->socketClient->setConnectTimeout($timeout);
129 1
    }
130
131
    /**
132
     * Returns the connect-timeout
133
     *
134
     * @return int
135
     */
136 1
    public function getConnectTimeout()
137
    {
138 1
        return $this->socketClient->getConnectTimeout();
139
    }
140
}
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...
141