Completed
Push — master ( dad17b...4ebd3a )
by Nicolas
05:03
created

AbstractTransport::sanityzeQueryStringBool()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 4
nop 1
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
     * @return mixed
69
     */
70
    public function sanityzeQueryStringBool($query)
71
    {
72
        foreach ($query as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $query of type string is not traversable.
Loading history...
73
            if (is_bool($value)) {
74
                $query[$key] = ($value) ? 'true' : 'false';
75
            }
76
        }
77
        return $query;
78
    }
79
80
    /**
81
     * Create a transport.
82
     *
83
     * The $transport parameter can be one of the following values:
84
     *
85
     * * string: The short name of a transport. For instance "Http"
86
     * * object: An already instantiated instance of a transport
87
     * * array: An array with a "type" key which must be set to one of the two options. All other
88
     *          keys in the array will be set as parameters in the transport instance
89
     *
90
     * @param mixed                $transport  A transport definition
91
     * @param \Elastica\Connection $connection A connection instance
92
     * @param array                $params     Parameters for the transport class
93
     *
94
     * @throws \Elastica\Exception\InvalidException
95
     *
96
     * @return AbstractTransport
97
     */
98
    public static function create($transport, Connection $connection, array $params = [])
99
    {
100
        if (is_array($transport) && isset($transport['type'])) {
101
            $transportParams = $transport;
102
            unset($transportParams['type']);
103
104
            $params = array_replace($params, $transportParams);
105
            $transport = $transport['type'];
106
        }
107
108
        if (is_string($transport)) {
109
            $specialTransports = [
110
                'httpadapter' => 'HttpAdapter',
111
                'nulltransport' => 'NullTransport',
112
            ];
113
114
            if (isset($specialTransports[strtolower($transport)])) {
115
                $transport = $specialTransports[strtolower($transport)];
116
            } else {
117
                $transport = ucfirst($transport);
118
            }
119
            $classNames = ["Elastica\\Transport\\$transport", $transport];
120
            foreach ($classNames as $className) {
121
                if (class_exists($className)) {
122
                    $transport = new $className();
123
                    break;
124
                }
125
            }
126
        }
127
128
        if ($transport instanceof self) {
129
            $transport->setConnection($connection);
130
131
            foreach ($params as $key => $value) {
132
                $transport->setParam($key, $value);
133
            }
134
        } else {
135
            throw new InvalidException('Invalid transport');
136
        }
137
138
        return $transport;
139
    }
140
}
141