Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
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 / 16
0.00% covered (danger)
0.00%
0 / 6
72
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLogVariables
0.00% covered (danger)
0.00%
0 / 4
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    /** @inheritDoc */
48    public function isCachedResponse() {
49        return false;
50    }
51
52    /** @inheritDoc */
53    public function getElasticTookMs() {
54        return $this->response?->getData()['took'] ?? -1;
55    }
56
57    /**
58     * @return array
59     */
60    public function getLogVariables() {
61        return [
62            'queryType' => $this->queryType,
63            'tookMs' => $this->getTookMs(),
64        ] + $this->extra;
65    }
66
67    /**
68     * We could generate multiple items for each bulk update that was sent..but
69     * doesn't seem necessary (yet).
70     *
71     * @return array[]
72     */
73    public function getRequests() {
74        return [ $this->getLogVariables() ];
75    }
76}