1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Sminnee\StitchData\StitchApi; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Sends package data to StitchData |
7
|
|
|
*/ |
8
|
|
|
class StitchDataSender |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $accessToken = null; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $clientID = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $tableName = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var StitchApi |
27
|
|
|
*/ |
28
|
|
|
private $client = null; |
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
// Use environment defines for client ID and access token by default |
33
|
|
|
if (defined('STITCHDATA_CLIENT_ID')) { |
34
|
|
|
$this->clientID = STITCHDATA_CLIENT_ID; |
35
|
|
|
} |
36
|
|
|
if (defined('STITCHDATA_ACCESS_TOKEN')) { |
37
|
|
|
$this->accessToken = STITCHDATA_ACCESS_TOKEN; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set the client ID of your StitchData account. |
43
|
|
|
* @return $this, for use with fluent syntax |
|
|
|
|
44
|
|
|
*/ |
45
|
|
|
public function setClientID($clientID) |
46
|
|
|
{ |
47
|
|
|
$this->clientID = $clientID; |
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set the access token for the StitchData Import API |
53
|
|
|
* @return $this, for use with fluent syntax |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function setAccessToken($accessToken) |
56
|
|
|
{ |
57
|
|
|
$this->accessToken = $accessToken; |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set the table name to write packages to |
63
|
|
|
* @return $this, for use with fluent syntax |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
public function setTableName($tableName) |
66
|
|
|
{ |
67
|
|
|
$this->tableName = $tableName; |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the client ID of your StitchData account. |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getClientID() |
76
|
|
|
{ |
77
|
|
|
return $this->clientID; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the access token for the StitchData Import API |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getAccessToken() |
85
|
|
|
{ |
86
|
|
|
return $this->accessToken; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the table name to write packages to |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getTableName() |
94
|
|
|
{ |
95
|
|
|
return $this->tableName; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the StitchApi client instance |
100
|
|
|
* |
101
|
|
|
* @param StitchApi $client |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function setClient(StitchApi $client) |
105
|
|
|
{ |
106
|
|
|
$this->client = $client; |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get (and/or create and set) the StitchApi client instance, allowing for lazy loading in case API arguments |
112
|
|
|
* want to be changed after construction. |
113
|
|
|
* |
114
|
|
|
* @return StitchApi |
115
|
|
|
*/ |
116
|
|
|
public function getClient() |
117
|
|
|
{ |
118
|
|
|
// Allow lazy loading |
119
|
|
|
if (!$this->client) { |
120
|
|
|
$this->setClient(new StitchApi($this->getClientID(), $this->getAccessToken())); |
121
|
|
|
} |
122
|
|
|
return $this->client; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Send the given package to the StitchData API |
127
|
|
|
*/ |
128
|
|
|
public function sendAddon(Addon $package) |
129
|
|
|
{ |
130
|
|
|
// If unconfigured, silently no-op |
131
|
|
|
if (!$this->getClientID() || !$this->getAccessToken() || !$this->getTableName()) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->getClient()->pushRecords( |
136
|
|
|
$this->getTableName(), |
137
|
|
|
[ 'Name' ], |
138
|
|
|
[ |
139
|
|
|
$this->addonToJson($package) |
140
|
|
|
] |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function addonToJson(Addon $package) |
145
|
|
|
{ |
146
|
|
|
$tz = new DateTimeZone('UTC'); |
147
|
|
|
|
148
|
|
|
$data = $package->toMap(); |
149
|
|
|
unset($data['LastEdited']); |
150
|
|
|
unset($data['Created']); |
151
|
|
|
unset($data['ClassName']); |
152
|
|
|
unset($data['RecordClassName']); |
153
|
|
|
|
154
|
|
|
foreach (['Released', 'LastUpdated', 'LastBuilt'] as $field) { |
155
|
|
|
if ($package->$field) { |
156
|
|
|
$datetime = is_numeric($package->$field) ? "@" . $package->$field : $package->$field; |
157
|
|
|
$data[$field] = new Datetime($datetime, $tz); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// Ensure consistent typing, otherwise StitchData splits into Rating__bi (big int) and |
162
|
|
|
// Rating__do (double precision) |
163
|
|
|
$data['Rating'] = (int)$data['Rating']; |
164
|
|
|
|
165
|
|
|
if ($package->RatingDetails) { |
|
|
|
|
166
|
|
|
$data['RatingDetails'] = []; |
167
|
|
|
foreach (json_decode($package->RatingDetails, true) as $k => $v) { |
|
|
|
|
168
|
|
|
$data['RatingDetails'][] = [ |
169
|
|
|
'Name' => $k, |
170
|
|
|
'Value' => $v, |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$data['Versions'] = []; |
176
|
|
|
foreach ($package->Versions() as $version) { |
|
|
|
|
177
|
|
|
$versionData = $version->toMap(); |
178
|
|
|
unset($versionData['LastEdited']); |
179
|
|
|
unset($versionData['Created']); |
180
|
|
|
unset($versionData['ClassName']); |
181
|
|
|
unset($versionData['RecordClassName']); |
182
|
|
|
unset($versionData['AddonID']); |
183
|
|
|
|
184
|
|
|
if ($version->Released) { |
185
|
|
|
$versionData['Released'] = new Datetime($version->Released, $tz); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
foreach ($version->CompatibleVersions() as $compatible) { |
189
|
|
|
$versionData['CompatibleVersions'][] = [ |
190
|
|
|
"Version" => $compatible->Name, |
191
|
|
|
"Major" => $compatible->Major, |
192
|
|
|
"Minor" => $compatible->Minor, |
193
|
|
|
]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
foreach ($version->Authors() as $author) { |
197
|
|
|
$authorData = $author->toMap(); |
198
|
|
|
unset($authorData['ID']); |
199
|
|
|
unset($authorData['ClassName']); |
200
|
|
|
unset($authorData['RecordClassName']); |
201
|
|
|
unset($authorData['LastEdited']); |
202
|
|
|
unset($authorData['Created']); |
203
|
|
|
$versionData['Authors'][] = $authorData; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$data['Versions'][] = $versionData; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $data; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.