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