| 199 |
lars |
1 |
<?php namespace Clockwork\Request;
|
|
|
2 |
|
|
|
3 |
// Filter requests before recording
|
|
|
4 |
class ShouldRecord
|
|
|
5 |
{
|
|
|
6 |
// Enable collecting of errors only (requests with 4xx or 5xx responses)
|
|
|
7 |
protected $errorsOnly = false;
|
|
|
8 |
// Enable collecting of slow requests only, slow response time threshold in ms
|
|
|
9 |
protected $slowOnly = false;
|
|
|
10 |
|
|
|
11 |
// Custom filter callback
|
|
|
12 |
protected $callback;
|
|
|
13 |
|
|
|
14 |
// Merge multiple settings from array
|
|
|
15 |
public function merge(array $data = [])
|
|
|
16 |
{
|
|
|
17 |
foreach ($data as $key => $val) $this->$key = $val;
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
// Apply the filter to a request
|
|
|
21 |
public function filter(Request $request)
|
|
|
22 |
{
|
|
|
23 |
return $this->passErrorsOnly($request)
|
|
|
24 |
&& $this->passSlowOnly($request)
|
|
|
25 |
&& $this->passCallback($request);
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
protected function passErrorsOnly(Request $request)
|
|
|
29 |
{
|
|
|
30 |
if (! $this->errorsOnly) return true;
|
|
|
31 |
|
|
|
32 |
return 400 <= $request->responseStatus && $request->responseStatus <= 599;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
protected function passSlowOnly(Request $request)
|
|
|
36 |
{
|
|
|
37 |
if (! $this->slowOnly) return true;
|
|
|
38 |
|
|
|
39 |
return $request->getResponseDuration() >= $this->slowOnly;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
protected function passCallback(Request $request)
|
|
|
43 |
{
|
|
|
44 |
if (! $this->callback) return true;
|
|
|
45 |
|
|
|
46 |
return call_user_func($this->callback, $request);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
// Fluent API
|
|
|
50 |
public function __call($method, $parameters)
|
|
|
51 |
{
|
|
|
52 |
if (! count($parameters)) return $this->$method;
|
|
|
53 |
|
|
|
54 |
$this->$method = count($parameters) ? $parameters[0] : true;
|
|
|
55 |
|
|
|
56 |
return $this;
|
|
|
57 |
}
|
|
|
58 |
}
|