createTransport()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 6
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
namespace Elastification\Client\Transport\Thrift;
19
20
use Elasticsearch\RestClient;
21
use Thrift\Protocol\TBinaryProtocolAccelerated;
22
use Thrift\Transport\TBufferedTransport;
23
use Thrift\Transport\TFramedTransport;
24
use Thrift\Transport\TSocket;
25
26
/**
27
 * Factory class to create a thrift connection.
28
 *
29
 * Most of this factory code is borrowed from Ruflin's Elastica client.
30
 * {@see https://github.com/ruflin/Elastica/blob/master/lib/Elastica/Transport/Thrift.php}.
31
 *
32
 * @package Elastification\Client\Transport\Thrift
33
 * @author  Mario Mueller
34
 */
35
class ThriftTransportConnectionFactory
36
{
37
    /**
38
     * @param string  $host
39
     * @param integer $port
40
     * @param null    $sendTimeout
41
     * @param null    $receiveTimeout
42
     * @param bool    $framedTransport
43
     *
44
     * @return RestClient
45
     * @author Mario Mueller
46
     */
47
    public static function factory($host, $port, $sendTimeout = null, $receiveTimeout = null, $framedTransport = false)
48
    {
49
        $socket = self::createSocket($host, $port, $sendTimeout, $receiveTimeout);
50
        $transport = self::createTransport($socket, $framedTransport);
51
        $protocol = new TBinaryProtocolAccelerated($transport);
52
        $client = new RestClient($protocol);
53
        $transport->open();
54
55
        return $client;
56
    }
57
58
    /**
59
     * @param string $host
60
     * @param int    $port
61
     * @param int    $sendTimeout
62
     * @param int    $receiveTimeout
63
     *
64
     * @return TSocket
65
     * @author Mario Mueller
66
     */
67
    private static function createSocket($host, $port, $sendTimeout, $receiveTimeout)
68
    {
69
        $socket = new TSocket($host, $port, true);
70
        if (null !== $sendTimeout) {
71
            $socket->setSendTimeout($sendTimeout);
72
        }
73
74
        if (null !== $receiveTimeout) {
75
            $socket->setRecvTimeout($receiveTimeout);
76
        }
77
78
        return $socket;
79
    }
80
81
    /**
82
     * @param TSocket $socket
83
     * @param bool    $framedTransport
84
     *
85
     * @return TBufferedTransport|TFramedTransport
86
     * @author Mario Mueller
87
     */
88
    private static function createTransport(TSocket $socket, $framedTransport = false)
89
    {
90
        if ($framedTransport) {
91
            $transport = new TFramedTransport($socket);
92
        } else {
93
            $transport = new TBufferedTransport($socket);
94
        }
95
96
        return $transport;
97
    }
98
}
99