1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Speicher210\FastbillBundle\DataCollector; |
4
|
|
|
|
5
|
|
|
use Speicher210\Fastbill\Api\ApiCredentials; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Data collector for Fastbill API. |
12
|
|
|
*/ |
13
|
|
|
class FastbillDataCollector extends DataCollector |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The API credentials. |
17
|
|
|
* |
18
|
|
|
* @var ApiCredentials |
19
|
|
|
*/ |
20
|
|
|
protected $apiCredentials; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The transport collector. |
24
|
|
|
* |
25
|
|
|
* @var TransportCollector |
26
|
|
|
*/ |
27
|
|
|
protected $transportCollector; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constructor. |
31
|
|
|
* |
32
|
|
|
* @param ApiCredentials $apiCredentials The Fastbill API credentials. |
33
|
|
|
* @param TransportCollector $transportCollector The transport collector. |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ApiCredentials $apiCredentials, TransportCollector $transportCollector) |
36
|
|
|
{ |
37
|
|
|
$this->apiCredentials = $apiCredentials; |
38
|
|
|
$this->transportCollector = $transportCollector; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
45
|
|
|
{ |
46
|
|
|
$this->data = array( |
47
|
|
|
'credentials' => $this->collectApiCredentials(), |
48
|
|
|
'requests' => $this->collectRequests() |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Collect the API credentials. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
protected function collectApiCredentials() |
58
|
|
|
{ |
59
|
|
|
return array( |
60
|
|
|
'email' => $this->apiCredentials->getEmail(), |
61
|
|
|
'key' => $this->apiCredentials->getApiKey(), |
62
|
|
|
'account_hash' => $this->varToString($this->apiCredentials->getAccountHash()) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Collect the requests. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected function collectRequests() |
72
|
|
|
{ |
73
|
|
|
$requests = array_fill_keys( |
74
|
|
|
array('article', 'coupon', 'customer', 'invoice', 'subscription'), |
75
|
|
|
array( |
76
|
|
|
'calls' => 0, |
77
|
|
|
'time' => 0, |
78
|
|
|
'requests' => array() |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$transportRequests = $this->transportCollector->getRequests(); |
83
|
|
|
foreach ($transportRequests as $transportRequest) { |
84
|
|
|
$request = array_change_key_case($transportRequest['request'], CASE_LOWER); |
85
|
|
|
list($service, $method) = explode('.', $request['service'], 2); |
86
|
|
|
|
87
|
|
|
$requests[$service]['calls']++; |
88
|
|
|
$requests[$service]['time'] += $transportRequest['time']; |
89
|
|
|
$requests[$service]['requests'][] = array( |
90
|
|
|
'request' => $transportRequest['request'], |
91
|
|
|
'response' => $transportRequest['response'], |
92
|
|
|
'method' => $method, |
93
|
|
|
'time' => $transportRequest['time'] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $requests; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the credentials. |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function getCredentials() |
106
|
|
|
{ |
107
|
|
|
return $this->data['credentials']; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the requests. |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function getRequests() |
116
|
|
|
{ |
117
|
|
|
return $this->data['requests']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the total calls. |
122
|
|
|
* |
123
|
|
|
* @return integer |
124
|
|
|
*/ |
125
|
|
|
public function getTotalCalls() |
126
|
|
|
{ |
127
|
|
|
$totalCalls = 0; |
128
|
|
|
foreach ($this->data['requests'] as $services) { |
129
|
|
|
$totalCalls += $services['calls']; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $totalCalls; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the total time. |
137
|
|
|
* |
138
|
|
|
* @return integer |
139
|
|
|
*/ |
140
|
|
|
public function getTotalTime() |
141
|
|
|
{ |
142
|
|
|
$totalTime = 0; |
143
|
|
|
foreach ($this->data['requests'] as $services) { |
144
|
|
|
$totalTime += $services['time']; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $totalTime; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function getName() |
154
|
|
|
{ |
155
|
|
|
return 'speicher210_fastbill'; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|