1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Sminnee\StitchData\StitchApi; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Sends package data to StitchData |
7
|
|
|
*/ |
8
|
|
|
class StitchDataSender |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private $accessToken = null; |
12
|
|
|
private $clientID = null; |
13
|
|
|
private $tableName = null; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
// Use environment defines for client ID and access token by default |
18
|
|
|
if (defined('STITCHDATA_CLIENT_ID')) { |
19
|
|
|
$this->clientID = STITCHDATA_CLIENT_ID; |
20
|
|
|
} |
21
|
|
|
if (defined('STITCHDATA_ACCESS_TOKEN')) { |
22
|
|
|
$this->accessToken = STITCHDATA_ACCESS_TOKEN; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set the client ID of your StitchData account. |
28
|
|
|
* @return $this, for use with fluent syntax |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
public function setClientID($clientID) |
31
|
|
|
{ |
32
|
|
|
$this->clientID = $clientID; |
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the access token for the StitchData Import API |
38
|
|
|
* @return $this, for use with fluent syntax |
|
|
|
|
39
|
|
|
*/ |
40
|
|
|
public function setAccessToken($accessToken) |
41
|
|
|
{ |
42
|
|
|
$this->accessToken = $accessToken; |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set the table name to write packages to |
48
|
|
|
* @return $this, for use with fluent syntax |
|
|
|
|
49
|
|
|
*/ |
50
|
|
|
public function setTableName($tableName) |
51
|
|
|
{ |
52
|
|
|
$this->tableName = $tableName; |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the client ID of your StitchData account. |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getClientID() |
61
|
|
|
{ |
62
|
|
|
return $this->clientID; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the access token for the StitchData Import API |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getAccessToken() |
70
|
|
|
{ |
71
|
|
|
return $this->accessToken; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the table name to write packages to |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getTableName() |
79
|
|
|
{ |
80
|
|
|
return $this->tableName; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Send the given package to the StitchData API |
85
|
|
|
*/ |
86
|
|
|
public function sendAddon(Addon $package) |
87
|
|
|
{ |
88
|
|
|
// If unconfigured, silently no-op |
89
|
|
|
if (!$this->clientID || !$this->accessToken || !$this->tableName) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$stitch = new StitchApi($this->clientID, $this->accessToken); |
94
|
|
|
|
95
|
|
|
$stitch->pushRecords( |
96
|
|
|
$this->tableName, |
97
|
|
|
[ 'Name' ], |
98
|
|
|
[ |
99
|
|
|
$this->addonToJson($package) |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function addonToJson(Addon $package) |
105
|
|
|
{ |
106
|
|
|
$tz = new DateTimeZone('UTC'); |
107
|
|
|
|
108
|
|
|
$data = $package->toMap(); |
109
|
|
|
unset($data['LastEdited']); |
110
|
|
|
unset($data['Created']); |
111
|
|
|
unset($data['ClassName']); |
112
|
|
|
unset($data['RecordClassName']); |
113
|
|
|
|
114
|
|
|
foreach (['Released', 'LastUpdated', 'LastBuilt'] as $field) { |
115
|
|
|
if ($package->$field) { |
116
|
|
|
$datetime = is_numeric($package->$field) ? "@" . $package->$field : $package->$field; |
117
|
|
|
$data[$field] = new Datetime($datetime, $tz); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($package->RatingDetails) { |
|
|
|
|
122
|
|
|
$data['RatingDetails'] = []; |
123
|
|
|
foreach (json_decode($package->RatingDetails, true) as $k => $v) { |
|
|
|
|
124
|
|
|
$data['RatingDetails'][] = [ |
125
|
|
|
'Name' => $k, |
126
|
|
|
'Value' => $v, |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$data['Versions'] = []; |
132
|
|
|
foreach ($package->Versions() as $version) { |
|
|
|
|
133
|
|
|
$versionData = $version->toMap(); |
134
|
|
|
unset($versionData['LastEdited']); |
135
|
|
|
unset($versionData['Created']); |
136
|
|
|
unset($versionData['ClassName']); |
137
|
|
|
unset($versionData['RecordClassName']); |
138
|
|
|
unset($versionData['AddonID']); |
139
|
|
|
|
140
|
|
|
if ($version->Released) { |
141
|
|
|
$versionData['Released'] = new Datetime($version->Released, $tz); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
foreach ($version->CompatibleVersions() as $compatible) { |
145
|
|
|
$versionData['CompatibleVersions'][] = [ |
146
|
|
|
"Version" => $compatible->Name, |
147
|
|
|
"Major" => $compatible->Major, |
148
|
|
|
"Minor" => $compatible->Minor, |
149
|
|
|
]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
foreach ($version->Authors() as $author) { |
153
|
|
|
$authorData = $author->toMap(); |
154
|
|
|
unset($authorData['ID']); |
155
|
|
|
unset($authorData['ClassName']); |
156
|
|
|
unset($authorData['RecordClassName']); |
157
|
|
|
unset($authorData['LastEdited']); |
158
|
|
|
unset($authorData['Created']); |
159
|
|
|
$versionData['Authors'][] = $authorData; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$data['Versions'][] = $versionData; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $data; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
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.