1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apix\Log; |
4
|
|
|
|
5
|
|
|
use Apix\Log\Emitter\EmitterInterface as LogEmitter; |
6
|
|
|
use Psr\Log\InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Dashbot logger for Apix Log. |
10
|
|
|
* |
11
|
|
|
* @see https://www.dashbot.io/sdk/generic |
12
|
|
|
*/ |
13
|
|
|
class Dashbot extends AbstractTracker |
14
|
|
|
{ |
15
|
|
|
const TRACKER_URL = |
16
|
|
|
'https://tracker.dashbot.io/track?platform=%s&v=%s&type=%s&apiKey=%s'; |
17
|
|
|
|
18
|
|
|
const TRANSPORTER_CMD = |
19
|
|
|
'curl -X POST -d %1$s \'%2$s\' -H \'Content-Type: application/json\''; |
20
|
|
|
|
21
|
|
|
const DEFAULT_PARAMS = array( |
22
|
|
|
'platform' => 'generic', // Either generic, facebook, slack, kik |
23
|
|
|
'v' => '0.7.4-rest', // API Version |
24
|
|
|
'type' => null, // Hit type (required) |
25
|
|
|
'apiKey' => null, // API key (required) |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
$mixed, LogEmitter $emitter = null, LogFormatter $formatter = null |
35
|
|
|
) { |
36
|
|
|
$this->setEmitter( |
37
|
|
|
$emitter ? $emitter : new Emitter\Async(self::TRANSPORTER_CMD), |
38
|
|
|
$formatter ? $formatter : new LogFormatter\Json() |
39
|
|
|
); |
40
|
|
|
$this->emitter->setParams(self::DEFAULT_PARAMS); |
41
|
|
|
|
42
|
|
|
if (is_array($mixed) && isset($mixed['apiKey'])) { |
43
|
|
|
$this->emitter->addParams($mixed); |
44
|
|
|
} elseif (is_string($mixed)) { |
45
|
|
|
$this->emitter->setParam('apiKey', $mixed); |
46
|
|
|
} else { |
47
|
|
|
throw new InvalidArgumentException(sprintf( |
48
|
|
|
'%s expects `apiKey` to be set, got: %s.', |
49
|
|
|
__CLASS__, json_encode($mixed) |
50
|
|
|
)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets the platform (format). |
56
|
|
|
* |
57
|
|
|
* @see https://www.dashbot.io/sdk/template |
58
|
|
|
* |
59
|
|
|
* @param string $platform Either 'generic', 'facebook', 'slack', 'kik' |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
|
|
public function setPlatform($platform) |
64
|
|
|
{ |
65
|
|
|
$this->emitter->setParam('platform', $platform); |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Sets a global tag (used to combined metrics). |
72
|
|
|
* |
73
|
|
|
* @see https://www.dashbot.io/sdk/template |
74
|
|
|
* |
75
|
|
|
* @param array $entries |
|
|
|
|
76
|
|
|
* @param string $local_tag |
|
|
|
|
77
|
|
|
* |
78
|
|
|
* @return self |
79
|
|
|
*/ |
80
|
|
|
public function setGlobalTag($global_tag = null) |
81
|
|
|
{ |
82
|
|
|
$this->emitter->setParam('dashbotTemplateId', $global_tag); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Rewrite the dashbot template Id. |
89
|
|
|
* |
90
|
|
|
* @see https://www.dashbot.io/sdk/template |
91
|
|
|
* |
92
|
|
|
* @param array $entries |
93
|
|
|
* @param array $params |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function rewriteTemplateId(array $entries, array $params) |
98
|
|
|
{ |
99
|
|
|
if (isset($entries['json']) && $params['platform'] == 'facebook') { |
100
|
|
|
if ($tpl_id = // get from `entries` (local) then `params` (global). |
|
|
|
|
101
|
|
|
$entries['dashbotTemplateId'] ?: $params['dashbotTemplateId'] |
102
|
|
|
?: false |
103
|
|
|
) { |
104
|
|
|
$entries['json']['dashbotTemplateId'] = $tpl_id; |
105
|
|
|
// remove from `entries` (local) to avoid duplicate. |
106
|
|
|
unset($entries['dashbotTemplateId']); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $entries; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Sets the event hit type. |
115
|
|
|
* |
116
|
|
|
* @param string $type |
117
|
|
|
* @param array $entries |
118
|
|
|
* @param string $local_tag |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
protected function get($type, array $entries, $local_tag = null) |
123
|
|
|
{ |
124
|
|
|
if ($local_tag) { |
|
|
|
|
125
|
|
|
$entries['dashbotTemplateId'] = $local_tag; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$entries = $this->rewriteTemplateId( |
129
|
|
|
$entries, $this->emitter->getParams() |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$this->emitter->setParam('type', $type); |
133
|
|
|
$this->emitter->setUrl(self::TRACKER_URL); |
134
|
|
|
|
135
|
|
|
return (array) $entries; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns an incoming tracking dataset. |
140
|
|
|
* |
141
|
|
|
* @param array $entries |
142
|
|
|
* @param string $local_tag |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function incoming(array $entries, $local_tag = null) |
147
|
|
|
{ |
148
|
|
|
return $this->get(__FUNCTION__, $entries, $local_tag); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns an outgoing tracking dataset. |
153
|
|
|
* |
154
|
|
|
* @param array $entries |
155
|
|
|
* @param string $local_tag |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
public function outgoing(array $entries, $local_tag = null) |
160
|
|
|
{ |
161
|
|
|
return $this->get(__FUNCTION__, $entries, $local_tag); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.