Completed
Push — master ( d6f147...044be5 )
by Nicolas
64:15 queued 59:52
created

NullTransport::generateDefaultResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Elastica\Transport;
3
4
use Elastica\JSON;
5
use Elastica\Request;
6
use Elastica\Response;
7
8
/**
9
 * Elastica Null Transport object.
10
 *
11
 * This is used in case you just need a test transport that doesn't do any connection to an elasticsearch
12
 * host but still returns a valid response object
13
 *
14
 * @author James Boehmer <[email protected]>
15
 * @author Jan Domanski <[email protected]>
16
 */
17
class NullTransport extends AbstractTransport
18
{
19
    /**
20
     * Response you want to get from the transport
21
     *
22
     * @var Response Response
23
     */
24
    protected $_response = null;
25
26
    /**
27
     * Set response object the transport returns
28
     *
29
     * @param \Elastica\Response $response
0 ignored issues
show
Bug introduced by
There is no parameter named $response. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
30
     *
31
     * @return $this
32
     */
33
    public function getResponse()
34
    {
35
        return $this->_response;
36
    }
37
38
    /**
39
     * Set response object the transport returns
40
     *
41
     * @param \Elastica\Response $response
42
     *
43
     * @return $this
44
     */
45
    public function setResponse(Response $response)
46
    {
47
        $this->_response = $response;
48
        return $this->_response;
49
    }
50
51
    /**
52
     * Generate an example response object
53
     *
54
     * @param array             $params  Hostname, port, path, ...
55
     *
56
     * @return \Elastica\Response $response
57
     */
58
    public function generateDefaultResponse(array $params)
59
    {
60
        $response = [
61
            'took' => 0,
62
            'timed_out' => false,
63
            '_shards' => [
64
                'total' => 0,
65
                'successful' => 0,
66
                'failed' => 0,
67
            ],
68
            'hits' => [
69
                'total' => 0,
70
                'max_score' => null,
71
                'hits' => [],
72
            ],
73
            'params' => $params,
74
        ];
75
        return new Response(JSON::stringify($response));
76
    }
77
78
    /**
79
     * Null transport.
80
     *
81
     * @param \Elastica\Request $request
82
     * @param array             $params  Hostname, port, path, ...
83
     *
84
     * @return \Elastica\Response Response empty object
85
     */
86
    public function exec(Request $request, array $params)
87
    {
88
        $response = $this->getResponse();
89
90
        if (!$response) {
91
            $response = $this->generateDefaultResponse($params);
92
        }
93
94
        return $response;
95
    }
96
}
97