Completed
Push — master ( 8a6a3b...86b2ed )
by Nicolas
03:28
created

AbstractTransport::create()   B

Complexity

Conditions 9
Paths 42

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 7.6924
c 0
b 0
f 0
cc 9
nc 42
nop 3
1
<?php
2
namespace Elastica\Transport;
3
4
use Elastica\Connection;
5
use Elastica\Exception\InvalidException;
6
use Elastica\Param;
7
use Elastica\Request;
8
9
/**
10
 * Elastica Abstract Transport object.
11
 *
12
 * @author Nicolas Ruflin <[email protected]>
13
 */
14
abstract class AbstractTransport extends Param
15
{
16
    /**
17
     * @var \Elastica\Connection
18
     */
19
    protected $_connection;
20
21
    /**
22
     * Construct transport.
23
     *
24
     * @param \Elastica\Connection $connection Connection object
25
     */
26
    public function __construct(Connection $connection = null)
27
    {
28
        if ($connection) {
29
            $this->setConnection($connection);
30
        }
31
    }
32
33
    /**
34
     * @return \Elastica\Connection Connection object
35
     */
36
    public function getConnection()
37
    {
38
        return $this->_connection;
39
    }
40
41
    /**
42
     * @param \Elastica\Connection $connection Connection object
43
     *
44
     * @return $this
45
     */
46
    public function setConnection(Connection $connection)
47
    {
48
        $this->_connection = $connection;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Executes the transport request.
55
     *
56
     * @param \Elastica\Request $request Request object
57
     * @param array             $params  Hostname, port, path, ...
58
     *
59
     * @return \Elastica\Response Response object
60
     */
61
    abstract public function exec(Request $request, array $params);
62
63
    /**
64
     * BOOL values true|false should be sanityzed and passed to Elasticsearch
65
     * as string.
66
     *
67
     * @param string $query
68
     *
69
     * @return mixed
70
     */
71
    public function sanityzeQueryStringBool($query)
72
    {
73
        foreach ($query as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $query of type string is not traversable.
Loading history...
74
            if (is_bool($value)) {
75
                $query[$key] = ($value) ? 'true' : 'false';
76
            }
77
        }
78
79
        return $query;
80
    }
81
82
    /**
83
     * Create a transport.
84
     *
85
     * The $transport parameter can be one of the following values:
86
     *
87
     * * string: The short name of a transport. For instance "Http"
88
     * * object: An already instantiated instance of a transport
89
     * * array: An array with a "type" key which must be set to one of the two options. All other
90
     *          keys in the array will be set as parameters in the transport instance
91
     *
92
     * @param mixed                $transport  A transport definition
93
     * @param \Elastica\Connection $connection A connection instance
94
     * @param array                $params     Parameters for the transport class
95
     *
96
     * @throws \Elastica\Exception\InvalidException
97
     *
98
     * @return AbstractTransport
99
     */
100
    public static function create($transport, Connection $connection, array $params = [])
101
    {
102
        if (is_array($transport) && isset($transport['type'])) {
103
            $transportParams = $transport;
104
            unset($transportParams['type']);
105
106
            $params = array_replace($params, $transportParams);
107
            $transport = $transport['type'];
108
        }
109
110
        if (is_string($transport)) {
111
            $specialTransports = [
112
                'httpadapter' => 'HttpAdapter',
113
                'nulltransport' => 'NullTransport',
114
            ];
115
116
            if (isset($specialTransports[strtolower($transport)])) {
117
                $transport = $specialTransports[strtolower($transport)];
118
            } else {
119
                $transport = ucfirst($transport);
120
            }
121
            $classNames = ["Elastica\\Transport\\$transport", $transport];
122
            foreach ($classNames as $className) {
123
                if (class_exists($className)) {
124
                    $transport = new $className();
125
                    break;
126
                }
127
            }
128
        }
129
130
        if ($transport instanceof self) {
131
            $transport->setConnection($connection);
132
133
            foreach ($params as $key => $value) {
134
                $transport->setParam($key, $value);
135
            }
136
        } else {
137
            throw new InvalidException('Invalid transport');
138
        }
139
140
        return $transport;
141
    }
142
}
143