1
|
|
|
<?php namespace JMathai\PhpMultiCurl; |
2
|
|
|
/** |
3
|
|
|
* MultiCurlSequence displays sequence of http calls |
4
|
|
|
* |
5
|
|
|
* @author Jaisen Mathai <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
class MultiCurlSequence |
8
|
|
|
{ |
9
|
|
|
private $width = 100; |
10
|
|
|
private $timers; |
11
|
|
|
private $min; |
12
|
|
|
private $max; |
13
|
|
|
private $range; |
14
|
|
|
private $step; |
15
|
|
|
|
16
|
|
|
public function __construct($timers) |
17
|
|
|
{ |
18
|
|
|
$this->timers = $timers; |
19
|
|
|
|
20
|
|
|
$min = PHP_INT_MAX; |
21
|
|
|
$max = 0; |
22
|
|
|
foreach($this->timers as $timer) |
23
|
|
|
{ |
24
|
|
|
if(!isset($timer['start'])) |
25
|
|
|
$timer['start'] = PHP_INT_MAX; |
26
|
|
|
|
27
|
|
|
if(!isset($timer['end'])) |
28
|
|
|
$timer['end'] = 0; |
29
|
|
|
|
30
|
|
|
$min = min($timer['start'], $min); |
31
|
|
|
$max = max($timer['end'], $max); |
32
|
|
|
} |
33
|
|
|
$this->min = $min; |
34
|
|
|
$this->max = $max; |
35
|
|
|
$this->range = $max-$min; |
36
|
|
|
$this->step = floatval($this->range/$this->width); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function renderAscii() |
40
|
|
|
{ |
41
|
|
|
$tpl = ''; |
42
|
|
|
foreach($this->timers as $timer) |
43
|
|
|
$tpl .= $this->tplAscii($timer); |
44
|
|
|
|
45
|
|
|
return $tpl; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function tplAscii($timer) |
49
|
|
|
{ |
50
|
|
|
$lpad = $rpad = 0; |
51
|
|
|
$lspace = $chars = $rspace = ''; |
52
|
|
View Code Duplication |
if($timer['start'] > $this->min) |
|
|
|
|
53
|
|
|
$lpad = intval(($timer['start'] - $this->min) / $this->step); |
54
|
|
View Code Duplication |
if($timer['end'] < $this->max) |
|
|
|
|
55
|
|
|
$rpad = intval(($this->max - $timer['end']) / $this->step); |
56
|
|
|
$mpad = $this->width - $lpad - $rpad; |
57
|
|
|
if($lpad > 0) |
58
|
|
|
$lspace = str_repeat(' ', $lpad); |
59
|
|
|
if($mpad > 0) |
60
|
|
|
$chars = str_repeat('=', $mpad); |
61
|
|
|
if($rpad > 0) |
62
|
|
|
$rspace = str_repeat(' ', $rpad); |
63
|
|
|
|
64
|
|
|
$tpl = <<<TPL |
65
|
|
|
({$timer['api']} :: code={$timer['code']}, start={$timer['start']}, end={$timer['end']}, total={$timer['time']}) |
66
|
|
|
[{$lspace}{$chars}{$rspace}] |
67
|
|
|
|
68
|
|
|
TPL; |
69
|
|
|
return $tpl; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.