1 | <?php |
||
18 | class PhraseanetSDKDataCollector extends DataCollector |
||
19 | { |
||
20 | /** |
||
21 | * @var HistoryPlugin |
||
22 | */ |
||
23 | private $profiler; |
||
24 | |||
25 | 2 | public function __construct(HistoryPlugin $profiler) |
|
29 | |||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | public function collect(Request $request, Response $response, \Exception $exception = null) |
||
34 | { |
||
35 | $this->data = array( |
||
36 | 'calls' => array(), |
||
37 | 'cache_hits' => 0, |
||
38 | 'error_count' => 0, |
||
39 | 'methods' => array(), |
||
40 | 'total_time' => 0, |
||
41 | ); |
||
42 | |||
43 | foreach ($this->profiler as $call) { |
||
44 | $error = false; |
||
45 | $request = $call; |
||
46 | $response = $request->getResponse(); |
||
47 | |||
48 | $requestContent = null; |
||
49 | if ($request instanceof EntityEnclosingRequestInterface) { |
||
50 | $requestContent = (string) $request->getBody(); |
||
51 | } |
||
52 | |||
53 | $time = array( |
||
54 | 'total' => $response->getInfo('total_time'), |
||
55 | 'connection' => $response->getInfo('connect_time'), |
||
56 | ); |
||
57 | |||
58 | |||
59 | $this->data['total_time'] += $response->getInfo('total_time'); |
||
60 | |||
61 | if (!isset($this->data['methods'][$request->getMethod()])) { |
||
62 | $this->data['methods'][$request->getMethod()] = 0; |
||
63 | } |
||
64 | |||
65 | $this->data['methods'][$request->getMethod()]++; |
||
66 | |||
67 | if ($response->isError()) { |
||
68 | $this->data['error_count']++; |
||
69 | $error = true; |
||
70 | } |
||
71 | |||
72 | if (substr($response->getHeaders()->get('X-Cache', ''), 0, 3) == 'HIT') { |
||
73 | $this->data['cache_hits'] += 1; |
||
74 | } |
||
75 | |||
76 | $decodedBody = json_decode($response->getBody(true)); |
||
77 | |||
78 | if (! $decodedBody) { |
||
79 | $decodedBody = $response->getBody('true'); |
||
80 | } |
||
81 | |||
82 | $this->data['calls'][] = array( |
||
83 | 'request' => $this->sanitizeRequest($request), |
||
84 | 'requestContent' => $requestContent, |
||
85 | 'response' => $this->sanitizeResponse($response), |
||
86 | 'responseContent' => $decodedBody, |
||
87 | 'time' => $time, |
||
88 | 'error' => $error, |
||
89 | 'phraseanet' => $this->parsePhraseanetResponse($decodedBody, $response) |
||
90 | ); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return array |
||
96 | */ |
||
97 | public function getCalls() |
||
101 | |||
102 | /** |
||
103 | * @return int |
||
104 | */ |
||
105 | public function countErrors() |
||
109 | |||
110 | /** |
||
111 | * @return array |
||
112 | */ |
||
113 | public function getMethods() |
||
117 | |||
118 | /** |
||
119 | * @return int |
||
120 | */ |
||
121 | public function getTotalTime() |
||
125 | |||
126 | /** |
||
127 | * @return int|void |
||
128 | */ |
||
129 | public function getCacheHitRatio() |
||
130 | { |
||
131 | $totalCalls = count($this->getCalls()); |
||
132 | |||
133 | if (! isset($this->data['cache_hits']) || $totalCalls == 0) { |
||
134 | return 0; |
||
135 | } |
||
136 | |||
137 | return $this->data['cache_hits'] * 100 / $totalCalls; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 2 | public function getName() |
|
147 | |||
148 | /** |
||
149 | * @param RequestInterface $request |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | private function sanitizeRequest(RequestInterface $request) |
||
169 | |||
170 | /** |
||
171 | * @param GuzzleResponse $response |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | private function sanitizeResponse($response) |
||
183 | |||
184 | private function parsePhraseanetResponse($data, $response) |
||
210 | } |
||
211 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.