Skip to content

Commit ff89bf8

Browse files
committed
some code cleanup.
1 parent 1bd3c35 commit ff89bf8

File tree

5 files changed

+66
-76
lines changed

5 files changed

+66
-76
lines changed

src/AnalyticsKit/AnalyticsKit.php

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
use HMS\AccountKit\AccountKit;
55
use HMS\Core\Wrapper;
6-
use InvalidArgumentException;
7-
use stdClass;
86

97
/**
108
* Class HMS AnalyticsKit Wrapper
@@ -13,7 +11,6 @@
1311
* @author Martin Zeitler
1412
*/
1513
class AnalyticsKit extends Wrapper {
16-
1714
private string|null $url_user_data_export;
1815
private string|null $url_user_data_export_status;
1916
private string|null $url_user_data_deletion;
@@ -27,7 +24,6 @@ class AnalyticsKit extends Wrapper {
2724
private string|null $url_report_dimensions_list;
2825

2926
public function __construct( array $config ) {
30-
3127
parent::__construct( $config );
3228
$this->post_init();
3329

@@ -59,113 +55,106 @@ protected function auth_headers(): array {
5955
return [
6056
"Content-Type: application/json; charset=utf-8",
6157
"Authorization: Bearer $this->access_token",
62-
"x-product-id: $this->product_id",
63-
"x-app-id: $this->oauth2_client_id"
58+
"x-app-id: $this->oauth2_client_id",
59+
"x-product-id: $this->product_id"
6460
];
6561
}
6662

6763
/**
6864
* Exporting Personal Data.
69-
*
7065
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/android-api-export-personal-data-0000001050987229">Exporting Personal Data</a>
71-
* @param string|null $aaid
72-
* @return stdClass
66+
* @param string|null $aaid AAID, which can be obtained using the API on the device side.
67+
* @return \stdClass
7368
*/
74-
public function request_user_data_export( string|null $aaid ): stdClass {
69+
public function request_user_data_export( ?string $aaid=null ): \stdClass {
7570
$payload = [];
7671
if ($aaid != null) {$payload = ['aaid' => $aaid];}
77-
return $this->request('POST', $this->url_user_data_export, parent::auth_headers(), $payload);
72+
return $this->request('POST', $this->url_user_data_export, $this->auth_headers(), $payload);
7873
}
7974

8075
/**
8176
* Querying the Export Task Status.
82-
*
8377
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/android-api-query-export-task-status-new-0000001135560119">Querying the Export Task Status</a>
84-
* @param ?string $aaid
85-
* @return stdClass
78+
* @param string|null $aaid AAID, which can be obtained using the API on the device side.
79+
* @return \stdClass
8680
*/
87-
public function request_user_data_export_status( ?string $aaid ): stdClass {
81+
public function request_user_data_export_status( ?string $aaid=null ): \stdClass {
8882
$payload = [];
8983
if ($aaid != null) {$payload = ['aaid' => $aaid];}
90-
return $this->request('POST', $this->url_user_data_export_status, parent::auth_headers(), $payload);
84+
return $this->request('POST', $this->url_user_data_export_status, $this->auth_headers(), $payload);
9185
}
9286

9387
/**
9488
* Deleting Personal Data.
95-
*
9689
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/android-api-delete-personal-data-0000001050747213">Deleting Personal Data</a>
97-
* @param ?string $aaid
98-
* @return stdClass
90+
* @param string|null $aaid AAID, which can be obtained using the API on the device side.
91+
* @return \stdClass
9992
*/
100-
public function request_user_data_deletion( ?string $aaid ): stdClass {
93+
public function request_user_data_deletion( ?string $aaid=null ): \stdClass {
10194
$payload = [];
10295
if ($aaid != null) {$payload = ['aaid' => $aaid];}
103-
return $this->request('POST', $this->url_user_data_deletion, parent::auth_headers(), $payload);
96+
return $this->request('POST', $this->url_user_data_deletion, $this->auth_headers(), $payload);
10497
}
10598

10699
/**
107100
* Querying the Deletion Task Status.
108-
*
109101
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/android-api-query-deletion-task-status-new-0000001088418298">Querying the Deletion Task Status</a>
110-
* @param ?string $aaid
111-
* @return stdClass
102+
* @param string|null $aaid AAID, which can be obtained using the API on the device side.
103+
* @return \stdClass
112104
*/
113-
public function request_user_data_deletion_status( ?string $aaid ): stdClass {
105+
public function request_user_data_deletion_status( ?string $aaid ): \stdClass {
114106
$payload = [];
115107
if ($aaid != null) {$payload = ['aaid' => $aaid];}
116-
return $this->request('POST', $this->url_user_data_deletion_status, parent::auth_headers(), $payload);
108+
return $this->request('POST', $this->url_user_data_deletion_status, $this->auth_headers(), $payload);
117109
}
118110

119111
/**
120112
* Creating a Data Export Task.
121-
*
122113
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/android-api-create-data-export-task-0000001050987231#section195846692412">Creating a Data Export Task</a>
123-
* @param ?string $aaid
124-
* @return stdClass
114+
* @param string|null $aaid AAID, which can be obtained using the API on the device side.
115+
* @return \stdClass
125116
*/
126-
public function request_raw_data_export( ?string $aaid ): stdClass {
117+
public function request_raw_data_export( ?string $aaid ): \stdClass {
127118
$payload = [];
128119
if ($aaid != null) {$payload = ['aaid' => $aaid];}
129-
return $this->request('POST', $this->url_user_data_export, parent::auth_headers(), $payload);
120+
return $this->request('POST', $this->url_user_data_export, $this->auth_headers(), $payload);
130121
}
131122

132123
/**
133124
* Receiving the Execution Status of a Data Export Task.
134125
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/android-api-create-data-export-task-0000001050987231#section195846692412">Receiving the Execution Status of a Data Export Task</a>
135-
* @return stdClass
126+
* @return \stdClass
136127
*/
137-
public function receive_raw_data_export_status( ): stdClass {
138-
return $this->request('POST', $this->url_user_data_export_status, parent::auth_headers(), []);
128+
public function receive_raw_data_export_status( ): \stdClass {
129+
return $this->request('POST', $this->url_user_data_export_status, $this->auth_headers(), []);
139130
}
140131

141132
/**
142133
* Importing Custom User Attributes.
143-
*
144134
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/android-api-import-customized-user-attributes-0000001050747215">Importing Custom User Attributes</a>
145135
* @param array $data
146-
* @return stdClass|false
136+
* @return \stdClass|false
147137
*/
148-
public function data_collection_import_user( array $data ): stdClass|false {
138+
public function data_collection_import_user( array $data ): \stdClass|false {
149139
if (sizeof($data) < 1 || sizeof($data) > 100) {
150-
throw new InvalidArgumentException('the userdata_set must have 1-100 items');
140+
throw new \InvalidArgumentException('the userdata_set must have 1-100 items');
151141
}
152142
$payload = [
153143
'data_type' => 1,
154144
'userdata_set' => $data
155145
];
156-
return $this->request('POST', $this->url_data_collection_import_user, parent::auth_headers(), $payload);
146+
return $this->request('POST', $this->url_data_collection_import_user, $this->auth_headers(), $payload);
157147
}
158148

159149
/**
160150
* Importing Content.
161-
*
162151
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/android-api-import-content-0000001205881011">Importing Content</a>
163152
* @param array $data
164-
* @return stdClass
153+
* @return \stdClass
165154
*/
166-
public function data_collection_import_item( array $data ): stdClass {
155+
public function data_collection_import_item( array $data ): \stdClass {
167156
if (sizeof($data) < 1 || sizeof($data) > 100) {
168-
throw new InvalidArgumentException('the item_set must have 1-100 items');
157+
throw new \InvalidArgumentException('the item_set must have 1-100 items');
169158
}
170159
$payload = [
171160
'data_type' => 2,
@@ -176,14 +165,13 @@ public function data_collection_import_item( array $data ): stdClass {
176165

177166
/**
178167
* Reporting User Behavior.
179-
*
180168
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/android-api-report-user-behavior-0000001205631635">Reporting User Behavior</a>
181169
* @param array $data
182-
* @return stdClass
170+
* @return \stdClass
183171
*/
184-
public function data_collection_import_event( array $data ): stdClass {
172+
public function data_collection_import_event( array $data ): \stdClass {
185173
if (sizeof($data) < 1 || sizeof($data) > 100) {
186-
throw new InvalidArgumentException('the event_set must have 1-100 items');
174+
throw new \InvalidArgumentException('the event_set must have 1-100 items');
187175
}
188176
$payload = [
189177
'data_type' => 3,
@@ -198,16 +186,15 @@ public function data_collection_import_event( array $data ): stdClass {
198186

199187
/**
200188
* Querying Open Metrics and Dimensions.
201-
*
202189
* @param string $lang
203190
* @param int $size
204191
* @param int $curr_page
205-
* @return stdClass
192+
* @return \stdClass
206193
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/querying-open-indicators-and-dimensions-0000001152958663">Querying Open Metrics and Dimensions</a>
207194
*/
208-
public function query_metrics_and_dimensions( string $lang='en', int $size=10, int $curr_page=1 ): stdClass {
195+
public function query_metrics_and_dimensions( string $lang='en', int $size=10, int $curr_page=1 ): \stdClass {
209196
if (! in_array($lang, ['en', 'cn', 'ru'])) {
210-
throw new InvalidArgumentException('lang must must be one of: en, cn, ru');
197+
throw new \InvalidArgumentException('lang must must be one of: en, cn, ru');
211198
}
212199
$payload = [
213200
'lang' => $lang,
@@ -219,18 +206,17 @@ public function query_metrics_and_dimensions( string $lang='en', int $size=10, i
219206

220207
/**
221208
* Querying Dimension Values.
222-
*
223209
* @param string $metric
224210
* @param string $dimension
225211
* @param string $lang
226212
* @param int $size
227213
* @param int $from
228-
* @return stdClass
214+
* @return \stdClass
229215
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/querying-dimension-values-0000001106240464">Querying Dimension Values</a>
230216
*/
231-
public function query_dimensions( string $metric, string $dimension, string $lang='en', int $size=10, int $from=0 ): stdClass {
217+
public function query_dimensions( string $metric, string $dimension, string $lang='en', int $size=10, int $from=0 ): \stdClass {
232218
if (! in_array($lang, ['en', 'cn', 'ru'])) {
233-
throw new InvalidArgumentException('lang must must be one of: en, cn, ru');
219+
throw new \InvalidArgumentException('lang must must be one of: en, cn, ru');
234220
}
235221
$payload = [
236222
'metric_name' => $metric,
@@ -244,7 +230,6 @@ public function query_dimensions( string $metric, string $dimension, string $lan
244230

245231
/**
246232
* Querying Statistical Metrics.
247-
*
248233
* @param string $metric
249234
* @param array $dimensions
250235
* @param string|null $start_date
@@ -254,12 +239,12 @@ public function query_dimensions( string $metric, string $dimension, string $lan
254239
* @param string $lang
255240
* @param int $size
256241
* @param int $curr_page
257-
* @return stdClass
242+
* @return \stdClass
258243
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/querying-tatistical-indicators-0000001152959491">Querying Statistical Metrics</a>
259244
*/
260-
public function query_metrics( string $metric, array $dimensions, string|null $start_date, string|null $end_date, string|null $filters, string|null $order_by, string $lang='en', int $size=10, int $curr_page=1 ): stdClass {
245+
public function query_metrics( string $metric, array $dimensions, string|null $start_date, string|null $end_date, string|null $filters, string|null $order_by, string $lang='en', int $size=10, int $curr_page=1 ): \stdClass {
261246
if (! in_array($lang, ['en', 'cn', 'ru'])) {
262-
throw new InvalidArgumentException('lang must must be one of: en, cn, ru');
247+
throw new \InvalidArgumentException('lang must must be one of: en, cn, ru');
263248
}
264249
$payload = [
265250
'metric' => $metric,

tests/AccountKitTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ public function test_get_user_info() {
5656
$data = json_decode(file_get_contents(self::$user_access_token_path));
5757
self::$user_access_token = $data->access_token;
5858
$result = self::$client->get_user_info( self::$user_access_token );
59-
self::assertTrue( property_exists($result, 'displayName') );
6059
self::assertTrue( property_exists($result, 'openID') );
61-
self::assertTrue( property_exists($result, 'headPictureURL') );
60+
self::assertTrue( property_exists($result, 'displayName') );
6261
self::assertTrue( property_exists($result, 'displayNameFlag') );
62+
self::assertTrue( property_exists($result, 'headPictureURL') );
63+
echo print_r($result, true);
6364
} else {
6465
$this->markTestSkipped('File not found: ' . self::$user_access_token_path);
6566
}

tests/AdsKitTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public function test_publisher_report() {
2828
foreach (self::CURRENCIES as $currency) {
2929
$filtering->currency = $currency;
3030
$result = self::$client->publisher_report( self::START_DATE,self::END_DATE, $filtering );
31-
self::assertTrue( $result->code == 0, 'Not HTTP 200 OK' );
3231
self::assertTrue( property_exists($result, 'data') && is_object($result->data) );
3332
self::assertTrue( property_exists($result->data, 'page_info') && is_object($result->data->page_info) );
3433
self::assertTrue( property_exists($result->data, 'list') && is_array($result->data->list) );
34+
echo print_r($result->data, true);
3535
}
3636
}
3737
}

tests/AnalyticsKitTest.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
class AnalyticsKitTest extends BaseTestCase {
1313

1414
private static ?AnalyticsKit $client;
15-
private static ?string $test_aaid = null;
15+
private static ?string $test_aaid = 'test';
1616

1717
/** This method is called before the first test of this test class is run. */
1818
public static function setUpBeforeClass(): void {
19-
2019
parent::setUpBeforeClass();
21-
2220
self::$client = new AnalyticsKit( self::get_config() );
2321
self::assertTrue( self::$client->is_ready(), self::CLIENT_NOT_READY );
2422
}
@@ -63,27 +61,33 @@ public function test_raw_data_export() {
6361
}
6462

6563
/** TODO Test: Receiving the Execution Status of a Data Export Task; post-back. */
66-
public function test_raw_data_export_status() {
67-
68-
self::assertTrue( true );
64+
public function test_receive_raw_data_export_status() {
65+
$result = self::$client->receive_raw_data_export_status();
66+
self::assertTrue(property_exists($result, 'code'));
6967
}
7068

7169
/** TODO Test: Importing Custom User Attributes. */
7270
public function test_data_collection_import_user() {
71+
$result = self::$client->data_collection_import_user([
7372

74-
self::assertTrue( true );
73+
]);
74+
self::assertTrue(property_exists($result, 'code'));
7575
}
7676

7777
/** TODO Test: Importing Content. */
7878
public function test_data_collection_import_item() {
79+
$result = self::$client->data_collection_import_item([
7980

80-
self::assertTrue( true );
81+
]);
82+
self::assertTrue(property_exists($result, 'code'));
8183
}
8284

8385
/** TODO Test: Reporting User Behavior. */
8486
public function test_data_collection_import_event() {
87+
$result = self::$client->data_collection_import_event([
8588

86-
self::assertTrue( true );
89+
]);
90+
self::assertTrue(property_exists($result, 'code'));
8791
}
8892

8993
/** Test: Querying Open Metrics and Dimensions. */
@@ -96,13 +100,13 @@ public function test_query_metrics_and_dimensions() {
96100

97101
/** TODO Test: Querying Dimension Values. */
98102
public function test_query_dimensions() {
99-
100-
self::assertTrue( true );
103+
$result = self::$client->query_dimensions("x", "y" );
104+
self::assertTrue(property_exists($result, 'code'));
101105
}
102106

103107
/** TODO Test: Querying Statistical Metrics. */
104108
public function test_query_metrics() {
105-
106-
self::assertTrue( true );
109+
$result = self::$client->query_metrics("x", ["y"], null, null, null, null, 'en', 10);
110+
self::assertTrue(property_exists($result, 'code'));
107111
}
108112
}

tests/BaseTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected static function get_config(): array {
9191
}
9292

9393
/** It provides the user configuration array. */
94-
#[ArrayShape(['access_token' => 'string', 'debug_mode' => 'bool'])]
94+
#[ArrayShape(['oauth2_client_id' => 'int', 'product_id' => 'int','access_token' => 'string', 'debug_mode' => 'bool'])]
9595
protected static function get_user_config(): array {
9696
self::load_user_access_token();
9797
return [

0 commit comments

Comments
 (0)