Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
BaseRequestLog
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 7
132
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
 start
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 finish
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTookMs
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getQueryType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatDescription
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace CirrusSearch;
4
5abstract class BaseRequestLog implements RequestLog {
6    /**
7     * @var string
8     */
9    protected $description;
10
11    /**
12     * @var string
13     */
14    protected $queryType;
15
16    /**
17     * @var array
18     */
19    protected $extra;
20
21    /**
22     * @var float|null The timestamp, with ms precision, the request started at
23     */
24    protected $startTime;
25
26    /**
27     * @var float|null The timestamp, with ms precision, the request ended at
28     */
29    protected $endTime;
30
31    /**
32     * @param string $description
33     * @param string $queryType
34     * @param array $extra
35     */
36    public function __construct( $description, $queryType, array $extra = [] ) {
37        $this->description = $description;
38        $this->queryType = $queryType;
39        $this->extra = $extra;
40    }
41
42    public function start() {
43        $this->startTime = microtime( true );
44    }
45
46    public function finish() {
47        $this->endTime = microtime( true );
48    }
49
50    /**
51     * @return int|null
52     */
53    public function getTookMs() {
54        if ( $this->startTime && $this->endTime ) {
55            return intval( 1000 * ( $this->endTime - $this->startTime ) );
56        } else {
57            return null;
58        }
59    }
60
61    /**
62     * @return string
63     */
64    public function getDescription() {
65        return $this->description;
66    }
67
68    /**
69     * @return string
70     */
71    public function getQueryType() {
72        return $this->queryType;
73    }
74
75    /**
76     * Perform a quick and dirty replacement for $this->description
77     * when it's not going through monolog. It replaces {foo} with
78     * the value of foo
79     *
80     * @return string
81     */
82    public function formatDescription() {
83        $pairs = [];
84        // @todo inefficient, getLogVariables may be doing work
85        // that gets done multiple times.
86        foreach ( $this->getLogVariables() as $key => $value ) {
87            if ( is_scalar( $value ) ) {
88                $pairs['{' . $key . '}'] = $value;
89            }
90        }
91        return strtr( $this->description, $pairs );
92    }
93}