Completed
Push — master ( 8547ba...3e3a1a )
by Nicolas
02:50
created

Request::getContentType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Elastica;
3
4
use Elastica\Exception\InvalidException;
5
6
/**
7
 * Elastica Request object.
8
 *
9
 * @author Nicolas Ruflin <[email protected]>
10
 */
11
class Request extends Param
12
{
13
    const HEAD = 'HEAD';
14
    const POST = 'POST';
15
    const PUT = 'PUT';
16
    const GET = 'GET';
17
    const DELETE = 'DELETE';
18
    const DEFAULT_CONTENT_TYPE = 'application/json';
19
    const NDJSON_CONTENT_TYPE = 'application/x-ndjson';
20
21
    /**
22
     * @var \Elastica\Connection
23
     */
24
    protected $_connection;
25
26
    /**
27
     * Construct.
28
     *
29
     * @param string     $path       Request path
30
     * @param string     $method     OPTIONAL Request method (use const's) (default = self::GET)
31
     * @param array      $data       OPTIONAL Data array
32
     * @param array      $query      OPTIONAL Query params
33
     * @param Connection $connection
34
     * @param string     $contentType Content-Type sent with this request
35
     *
36
     * @return \Elastica\Request OPTIONAL Connection object
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
37
     */
38
    public function __construct($path, $method = self::GET, $data = [], array $query = [], Connection $connection = null, $contentType = self::DEFAULT_CONTENT_TYPE )
39
    {
40
        $this->setPath($path);
41
        $this->setMethod($method);
42
        $this->setData($data);
43
        $this->setQuery($query);
44
45
        if ($connection) {
46
            $this->setConnection($connection);
47
        }
48
        $this->setContentType($contentType);
49
    }
50
51
    /**
52
     * Sets the request method. Use one of the for consts.
53
     *
54
     * @param string $method Request method
55
     *
56
     * @return $this
57
     */
58
    public function setMethod($method)
59
    {
60
        return $this->setParam('method', $method);
61
    }
62
63
    /**
64
     * Get request method.
65
     *
66
     * @return string Request method
67
     */
68
    public function getMethod()
69
    {
70
        return $this->getParam('method');
71
    }
72
73
    /**
74
     * Sets the request data.
75
     *
76
     * @param array $data Request data
77
     *
78
     * @return $this
79
     */
80
    public function setData($data)
81
    {
82
        return $this->setParam('data', $data);
83
    }
84
85
    /**
86
     * Return request data.
87
     *
88
     * @return array Request data
89
     */
90
    public function getData()
91
    {
92
        return $this->getParam('data');
93
    }
94
95
    /**
96
     * Sets the request path.
97
     *
98
     * @param string $path Request path
99
     *
100
     * @return $this
101
     */
102
    public function setPath($path)
103
    {
104
        return $this->setParam('path', $path);
105
    }
106
107
    /**
108
     * Return request path.
109
     *
110
     * @return string Request path
111
     */
112
    public function getPath()
113
    {
114
        return $this->getParam('path');
115
    }
116
117
    /**
118
     * Return query params.
119
     *
120
     * @return array Query params
121
     */
122
    public function getQuery()
123
    {
124
        return $this->getParam('query');
125
    }
126
127
    /**
128
     * @param array $query
129
     *
130
     * @return $this
131
     */
132
    public function setQuery(array $query = [])
133
    {
134
        return $this->setParam('query', $query);
135
    }
136
137
    /**
138
     * @param \Elastica\Connection $connection
139
     *
140
     * @return $this
141
     */
142
    public function setConnection(Connection $connection)
143
    {
144
        $this->_connection = $connection;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Return Connection Object.
151
     *
152
     * @throws Exception\InvalidException If no valid connection was setted
153
     *
154
     * @return \Elastica\Connection
155
     */
156
    public function getConnection()
157
    {
158
        if (empty($this->_connection)) {
159
            throw new InvalidException('No valid connection object set');
160
        }
161
162
        return $this->_connection;
163
    }
164
165
    /**
166
     * Set the Content-Type of this request
167
     * @param string $contentType
168
     */
169
    public function setContentType($contentType)
170
    {
171
        return $this->setParam('contentType', $contentType);
172
    }
173
174
    /**
175
     * Get the Content-Type of this request
176
     */
177
    public function getContentType()
178
    {
179
        return $this->getParam('contentType');
180
    }
181
182
    /**
183
     * Sends request to server.
184
     *
185
     * @return \Elastica\Response Response object
186
     */
187
    public function send()
188
    {
189
        $transport = $this->getConnection()->getTransportObject();
190
191
        // Refactor: Not full toArray needed in exec?
192
        return $transport->exec($this, $this->getConnection()->toArray());
193
    }
194
195
    /**
196
     * @return array
197
     */
198
    public function toArray()
199
    {
200
        $data = $this->getParams();
201
        if ($this->_connection) {
202
            $data['connection'] = $this->_connection->getParams();
203
        }
204
205
        return $data;
206
    }
207
208
    /**
209
     * Converts request to curl request format.
210
     *
211
     * @return string
212
     */
213
    public function toString()
214
    {
215
        return JSON::stringify($this->toArray());
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    public function __toString()
222
    {
223
        return $this->toString();
224
    }
225
}
226