Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
BulkUpdateRequestLog
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 6
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 finish
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 isCachedResponse
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getElasticTookMs
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getLogVariables
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getRequests
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch;
4
5/**
6 * Request log for requests that update the elasticsearch cluster. All
7 * update requests are done through bulk actions.
8 */
9class BulkUpdateRequestLog extends BaseRequestLog {
10    /**
11     * @var \Elastica\Client
12     */
13    private $client;
14
15    /**
16     * @var \Elastica\Response|null
17     */
18    private $lastResponse;
19
20    /**
21     * @var \Elastica\Response|null
22     */
23    private $response;
24
25    /**
26     * @param \Elastica\Client $client
27     * @param string $description
28     * @param string $queryType
29     * @param array $extra
30     */
31    public function __construct( \Elastica\Client $client, $description, $queryType, array $extra = [] ) {
32        parent::__construct( $description, $queryType, $extra );
33        $this->client = $client;
34        $this->lastResponse = $client->getLastResponse();
35    }
36
37    public function finish() {
38        if ( $this->response ) {
39            throw new \RuntimeException( 'Finishing a log more than once' );
40        }
41        parent::finish();
42        $response = $this->client->getLastResponse();
43        $this->response = $response === $this->lastResponse ? null : $response;
44        $this->lastResponse = null;
45    }
46
47    public function isCachedResponse() {
48        return false;
49    }
50
51    public function getElasticTookMs() {
52        if ( $this->response ) {
53            $data = $this->response->getData();
54            if ( isset( $data['took'] ) ) {
55                return $data['took'];
56            }
57        }
58
59        return -1;
60    }
61
62    /**
63     * @return array
64     */
65    public function getLogVariables() {
66        return [
67            'queryType' => $this->queryType,
68            'tookMs' => $this->getTookMs(),
69        ] + $this->extra;
70    }
71
72    /**
73     * We could generate multiple items for each bulk update that was sent..but
74     * doesn't seem necessary (yet).
75     *
76     * @return array[]
77     */
78    public function getRequests() {
79        return [ $this->getLogVariables() ];
80    }
81}