|
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
|
|
|
$data[$field] = new Datetime($package->$field, $tz); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$data['RatingDetails'] = []; |
|
121
|
|
|
foreach (json_decode($package->RatingDetails, true) as $k => $v) { |
|
|
|
|
|
|
122
|
|
|
$data['RatingDetails'][] = [ |
|
123
|
|
|
'Name' => $k, |
|
124
|
|
|
'Value' => $v, |
|
125
|
|
|
]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$data['Versions'] = []; |
|
129
|
|
|
foreach ($package->Versions() as $version) { |
|
|
|
|
|
|
130
|
|
|
$versionData = $version->toMap(); |
|
131
|
|
|
unset($versionData['LastEdited']); |
|
132
|
|
|
unset($versionData['Created']); |
|
133
|
|
|
unset($versionData['ClassName']); |
|
134
|
|
|
unset($versionData['RecordClassName']); |
|
135
|
|
|
unset($versionData['AddonID']); |
|
136
|
|
|
|
|
137
|
|
|
if ($version->Released) { |
|
138
|
|
|
$versionData['Released'] = new Datetime($version->Released, $tz); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
foreach ($version->CompatibleVersions() as $compatible) { |
|
142
|
|
|
$versionData['CompatibleVersions'][] = [ |
|
143
|
|
|
"Version" => $compatible->Name, |
|
144
|
|
|
"Major" => $compatible->Major, |
|
145
|
|
|
"Minor" => $compatible->Minor, |
|
146
|
|
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
foreach ($version->Authors() as $author) { |
|
150
|
|
|
$authorData = $author->toMap(); |
|
151
|
|
|
unset($authorData['ID']); |
|
152
|
|
|
unset($authorData['ClassName']); |
|
153
|
|
|
unset($authorData['RecordClassName']); |
|
154
|
|
|
unset($authorData['LastEdited']); |
|
155
|
|
|
unset($authorData['Created']); |
|
156
|
|
|
$versionData['Authors'][] = $authorData; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$data['Versions'][] = $versionData; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $data; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
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.