Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Outgoing | |
0.00% |
0 / 16 |
|
0.00% |
0 / 5 |
42 | |
0.00% |
0 / 1 |
| attach_hooks | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| detach_hooks | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| start_timer | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| log | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Inspect\Monitor; |
| 4 | |
| 5 | class Outgoing implements Observable { |
| 6 | private $start_time = array(); |
| 7 | private $logs = array(); |
| 8 | |
| 9 | public function attach_hooks() { |
| 10 | add_filter( 'http_request_args', array( $this, 'start_timer' ), 10, 2 ); |
| 11 | add_action( 'http_api_debug', array( $this, 'log' ), 10, 5 ); |
| 12 | } |
| 13 | |
| 14 | public function detach_hooks() { |
| 15 | remove_filter( 'http_request_args', array( $this, 'start_timer' ), 10 ); |
| 16 | remove_action( 'http_api_debug', array( $this, 'log' ), 5 ); |
| 17 | } |
| 18 | |
| 19 | public function start_timer( $args, $url ) { |
| 20 | $this->start_time[ $url ] = microtime( true ); |
| 21 | return $args; |
| 22 | } |
| 23 | |
| 24 | public function log( $response, $context, $transport, $args, $url ) { |
| 25 | |
| 26 | $log = array( |
| 27 | 'url' => $url, |
| 28 | 'args' => $args, |
| 29 | 'duration' => floor( 1000 * ( microtime( true ) - $this->start_time[ $url ] ) ), |
| 30 | ); |
| 31 | |
| 32 | if ( is_wp_error( $response ) ) { |
| 33 | $log['error'] = $response; |
| 34 | } else { |
| 35 | $log['response'] = $response; |
| 36 | } |
| 37 | |
| 38 | $this->logs[] = $log; |
| 39 | } |
| 40 | |
| 41 | public function get() { |
| 42 | return $this->logs; |
| 43 | } |
| 44 | } |