1+ <?php
2+
3+ namespace Stackify \Log \Transport ;
4+
5+ use Stackify \Log \Entities \LogEntryInterface ;
6+
7+ abstract class AbstractApiTransport extends AbstractTransport
8+ {
9+
10+ private $ queue = array ();
11+ private $ commonOptions = array ('proxy ' );
12+ protected $ apiKey ;
13+ protected $ proxy ;
14+
15+ public function __construct ($ apiKey , array $ options = array ())
16+ {
17+ parent ::__construct ();
18+ $ this ->apiKey = $ apiKey ;
19+ // @TODO validate & merge
20+ $ this ->extractOption ($ options , 'proxy ' );
21+ // type, host, port, user, pass
22+ // @TODO shutdown case
23+ // register_shutdown_function(array($this, 'finish'));
24+ }
25+
26+ public function addEntry (LogEntryInterface $ logEntry )
27+ {
28+ $ this ->queue [] = $ this ->messageBuilder ->createLogMsg ($ logEntry );
29+ }
30+
31+ public function finish ()
32+ {
33+ if (!empty ($ this ->queue )) {
34+ $ json = $ this ->messageBuilder ->getApiMessage ($ this ->queue );
35+ $ this ->send ($ json );
36+ }
37+ }
38+
39+ protected abstract function send ($ data );
40+
41+ protected abstract function getAllowedOptions ();
42+
43+ protected function extractOption ($ options , $ optionName )
44+ {
45+ $ allowed = array_merge ($ this ->commonOptions , $ this ->getAllowedOptions ());
46+ if (isset ($ options [$ optionName ]) && in_array ($ optionName , $ allowed )) {
47+ $ this ->$ optionName = $ options [$ optionName ];
48+ }
49+ }
50+
51+ protected function getApiHeaders ()
52+ {
53+ return array (
54+ 'X-Stackify-PV ' => 'V1 ' ,
55+ 'X-Stackify-Key ' => $ this ->apiKey ,
56+ );
57+ }
58+
59+ }
0 commit comments