|
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->getClient(), $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
|
|
|
if ($package->RatingDetails) { |
|
|
|
|
|
|
162
|
|
|
$data['RatingDetails'] = []; |
|
163
|
|
|
foreach (json_decode($package->RatingDetails, true) as $k => $v) { |
|
|
|
|
|
|
164
|
|
|
$data['RatingDetails'][] = [ |
|
165
|
|
|
'Name' => $k, |
|
166
|
|
|
'Value' => $v, |
|
167
|
|
|
]; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$data['Versions'] = []; |
|
172
|
|
|
foreach ($package->Versions() as $version) { |
|
|
|
|
|
|
173
|
|
|
$versionData = $version->toMap(); |
|
174
|
|
|
unset($versionData['LastEdited']); |
|
175
|
|
|
unset($versionData['Created']); |
|
176
|
|
|
unset($versionData['ClassName']); |
|
177
|
|
|
unset($versionData['RecordClassName']); |
|
178
|
|
|
unset($versionData['AddonID']); |
|
179
|
|
|
|
|
180
|
|
|
if ($version->Released) { |
|
181
|
|
|
$versionData['Released'] = new Datetime($version->Released, $tz); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
foreach ($version->CompatibleVersions() as $compatible) { |
|
185
|
|
|
$versionData['CompatibleVersions'][] = [ |
|
186
|
|
|
"Version" => $compatible->Name, |
|
187
|
|
|
"Major" => $compatible->Major, |
|
188
|
|
|
"Minor" => $compatible->Minor, |
|
189
|
|
|
]; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
foreach ($version->Authors() as $author) { |
|
193
|
|
|
$authorData = $author->toMap(); |
|
194
|
|
|
unset($authorData['ID']); |
|
195
|
|
|
unset($authorData['ClassName']); |
|
196
|
|
|
unset($authorData['RecordClassName']); |
|
197
|
|
|
unset($authorData['LastEdited']); |
|
198
|
|
|
unset($authorData['Created']); |
|
199
|
|
|
$versionData['Authors'][] = $authorData; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$data['Versions'][] = $versionData; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $data; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
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.