1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472:
<?php
/*
Copyright Game Server Services, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
namespace GS2\Inbox;
use GS2\Core\Gs2Credentials as Gs2Credentials;
use GS2\Core\AbstractGs2Client as AbstractGs2Client;
use GS2\Core\Exception\NullPointerException as NullPointerException;
/**
* GS2-Inbox クライアント
*
* @author Game Server Services, inc. <contact@gs2.io>
* @copyright Game Server Services, Inc.
*
*/
class Gs2InboxClient extends AbstractGs2Client {
public static $ENDPOINT = 'inbox';
public static $VARIATION = 'a';
/**
* コンストラクタ
*
* @param string $region リージョン名
* @param Gs2Credentials $credentials 認証情報
* @param array $options オプション
*/
public function __construct($region, Gs2Credentials $credentials, $options = []) {
parent::__construct($region, $credentials, $options);
}
/**
* 受信ボックスリストを取得
*
* @param string $pageToken ページトークン
* @param integer $limit 取得件数
* @return array
* * items
* * array
* * inboxId => 受信ボックスID
* * ownerId => オーナーID
* * name => 受信ボックス名
* * serviceClass => サービスクラス
* * autoDelete => 自動削除設定
* * cooperationUrl => 連携用URL
* * createAt => 作成日時
* * nextPageToken => 次ページトークン
*/
public function describeInbox($pageToken = NULL, $limit = NULL) {
$query = [];
if($pageToken) $query['pageToken'] = $pageToken;
if($limit) $query['limit'] = $limit;
return $this->doGet(
'Gs2Inbox',
'DescribeInbox',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox',
$query);
}
/**
* 受信ボックスを作成<br>
* <br>
* GS2-Inbox を利用するにはまず受信ボックスを作成します。<br>
* 受信ボックスを作成後、受信ボックスにメッセージを送信することでメッセージを届けることができます。<br>
* 1つの受信ボックスで、複数のユーザのメッセージを扱うことができますので、ユーザ数分の受信ボックスを作成する必要はありません。<br>
*
* @param array $request
* * name => 受信ボックス名
* * serviceClass => サービスクラス
* * autoDelete => 自動削除設定
* * cooperationUrl => 連携用URL
* @return array
* * item
* * inboxId => 受信ボックスID
* * ownerId => オーナーID
* * name => 受信ボックス名
* * serviceClass => サービスクラス
* * autoDelete => 自動削除設定
* * cooperationUrl => 連携用URL
* * createAt => 作成日時
*/
public function createInbox($request) {
if(is_null($request)) throw new NullPointerException();
$body = [];
if(array_key_exists('name', $request)) $body['name'] = $request['name'];
if(array_key_exists('serviceClass', $request)) $body['serviceClass'] = $request['serviceClass'];
if(array_key_exists('autoDelete', $request)) $body['autoDelete'] = $request['autoDelete'];
if(array_key_exists('cooperationUrl', $request)) $body['cooperationUrl'] = $request['cooperationUrl'];
$query = [];
return $this->doPost(
'Gs2Inbox',
'CreateInbox',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox',
$body,
$query);
}
/**
* サービスクラスリストを取得
*
* @return array サービスクラス
*/
public function describeServiceClass() {
$query = [];
$result = $this->doGet(
'Gs2Inbox',
'DescribeServiceClass',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox/serviceClass',
$query);
return $result['items'];
}
/**
* 受信ボックスを取得
*
* @param array $request
* * inboxName => 受信ボックス名
* @return array
* * item
* * inboxId => 受信ボックスID
* * ownerId => オーナーID
* * name => 受信ボックス名
* * serviceClass => サービスクラス
* * autoDelete => 自動削除設定
* * cooperationUrl => 連携用URL
* * createAt => 作成日時
*
*/
public function getInbox($request) {
if(is_null($request)) throw new NullPointerException();
if(!array_key_exists('inboxName', $request)) throw new NullPointerException();
$query = [];
return $this->doGet(
'Gs2Inbox',
'GetInbox',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox/'. $request['inboxName'],
$query);
}
/**
* 受信ボックスのステータスを取得
*
* @param array $request
* * inboxName => 受信ボックス名
* @return array
* * status => 状態
*/
public function getInboxStatus($request) {
if(is_null($request)) throw new NullPointerException();
if(!array_key_exists('inboxName', $request)) throw new NullPointerException();
if(is_null($request['inboxName'])) throw new NullPointerException();
$query = [];
return $this->doGet(
'Gs2Inbox',
'GetInboxStatus',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox/'. $request['inboxName']. '/status',
$query);
}
/**
* 受信ボックスを更新
*
* @param array $request
* * inboxName => 受信ボックス名
* * serviceClass => サービスクラス
* * cooperationUrl => 連携用URL
* @return array
* * item
* * inboxId => 受信ボックスID
* * ownerId => オーナーID
* * name => 受信ボックス名
* * serviceClass => サービスクラス
* * autoDelete => 自動削除設定
* * cooperationUrl => 連携用URL
* * createAt => 作成日時
*/
public function updateInbox($request) {
if(is_null($request)) throw new NullPointerException();
if(!array_key_exists('inboxName', $request)) throw new NullPointerException();
if(is_null($request['inboxName'])) throw new NullPointerException();
$body = [];
if(array_key_exists('serviceClass', $request)) $body['serviceClass'] = $request['serviceClass'];
if(array_key_exists('cooperationUrl', $request)) $body['cooperationUrl'] = $request['cooperationUrl'];
$query = [];
return $this->doPut(
'Gs2Inbox',
'UpdateInbox',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox/'. $request['inboxName'],
$body,
$query);
}
/**
* 受信ボックスを削除
*
* @param array $request
* * inboxName => 受信ボックス名
*/
public function deleteInbox($request) {
if(is_null($request)) throw new NullPointerException();
if(!array_key_exists('inboxName', $request)) throw new NullPointerException();
if(is_null($request['inboxName'])) throw new NullPointerException();
$query = [];
return $this->doDelete(
'Gs2Inbox',
'DeleteInbox',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox/'. $request['inboxName'],
$query);
}
/**
* メッセージを送信<br>
* <br>
* メッセージを受信ボックスに送信します。<br>
* メッセージには本文との他に開封時通知を有効にするかのフラグ、既読状態のフラグといった情報が付加されています。<br>
* <br>
* 開封時通知を有効にすると、受信ボックスに設定された連携用URLにメッセージIDがPOSTリクエストで通知されます。<br>
* メッセージ送信時にも送信したメッセージIDが取得できますので、<br>
* 例えば、メッセージに課金用アイテムを添付したい場合は以下の様なメッセージを送信します。<br>
* <ul>
* <li>送信先: user-0001</li>
* <li>メッセージ本文: サーバ障害のお詫びです</li>
* <li>開封時通知: 有効</li>
* </ul>
* このAPIの戻り値に含まれるメッセージIDとユーザID、アイテムの内容(課金用アイテム)をひも付けて保存します。<br>
* <br>
* その後、ユーザがこのメッセージを開封すると、連携用URLにこのメッセージのメッセージIDが通知されます。<br>
* それを受けて、ユーザIDのアカウントにアイテムの内容(課金用アイテム)を付与します。<br>
* これで、メッセージにアイテムを添付することができます。<br>
* <br>
* また、連携用URLを呼び出した際にエラー応答することで、メッセージの開封を失敗させることができます。<br>
* これによって、持ち物がいっぱいの場合などにアイテムの付与に失敗しても再度開封処理を実行させることができます。<br>
* <br>
* 開封時のコールバックは通信障害などの理由により、コールバック先のサーバは正しく処理を行えたけれど、<br>
* GS2側のインフラにレスポンスが届かず、結果的に処理が失敗する可能性を考慮する必要があります。<br>
* この場合、複数回の開封コールバックが呼び出される可能性がありますので、コールバック処理は冪等性を持った形で実装するようにしてください。<br>
*
* @param array $request
* * inboxName => 受信ボックス名
* * userId => 宛先ユーザID
* * message => メッセージ本文
* * cooperation => 開封時に連携用URLを呼び出すか
* @return array
* * item
* * messageId => メッセージID
* * inboxId => 受信ボックスID
* * userId => 受信ユーザID
* * message => メッセージ本文
* * cooperation => 開封時に連携用URLを呼び出すか
* * date => 受信日時
* * read => 既読状態
*/
public function sendMessage($request) {
if(is_null($request)) throw new NullPointerException();
if(!array_key_exists('inboxName', $request)) throw new NullPointerException();
if(is_null($request['inboxName'])) throw new NullPointerException();
$body = [];
if(array_key_exists('userId', $request)) $body['userId'] = $request['userId'];
if(array_key_exists('message', $request)) $body['message'] = $request['message'];
if(array_key_exists('cooperation', $request)) $body['cooperation'] = $request['cooperation'];
$query = [];
return $this->doPost(
'Gs2Inbox',
'SendMessage',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox/'. $request['inboxName']. '/message',
$body,
$query);
}
/**
* メッセージリストを取得<br>
* <br>
* accessToken には {@link http://static.docs.gs2.io/php/auth/class-GS2.Auth.Gs2AuthClient.html#_login Gs2AuthClient::login()} でログインして取得したアクセストークンを指定してください。<br>
*
* @param array $request
* * inboxName => 受信ボックス名
* * accessToken => アクセストークン
* @param string $pageToken ページトークン
* @param integer $limit 取得件数
* @return array
* * items
* * array
* * messageId => メッセージID
* * inboxId => 受信ボックスID
* * userId => 受信ユーザID
* * message => メッセージ本文
* * cooperation => 開封時に連携用URLを呼び出すか
* * date => 受信日時
* * read => 既読状態
* * nextPageToken => 次ページトークン
*/
public function describeMessage($request, $pageToken = NULL, $limit = NULL) {
if(is_null($request)) throw new NullPointerException();
if(!array_key_exists('inboxName', $request)) throw new NullPointerException();
if(is_null($request['inboxName'])) throw new NullPointerException();
if(!array_key_exists('accessToken', $request)) throw new NullPointerException();
if(is_null($request['accessToken'])) throw new NullPointerException();
$query = [];
if($pageToken) $query['pageToken'] = $pageToken;
if($limit) $query['limit'] = $limit;
$extparams = [
'headers' => [
'X-GS2-ACCESS-TOKEN' => $request['accessToken']
]
];
return $this->doGet(
'Gs2Inbox',
'DescribeMessage',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox/'. $request['inboxName']. '/message',
$query,
$extparams);
}
/**
* メッセージを取得
*
* @param array $request
* * inboxName => 受信ボックス名
* * messageId => メッセージID
* @return array
* * item
* * messageId => メッセージID
* * inboxId => 受信ボックスID
* * userId => 受信ユーザID
* * message => メッセージ本文
* * cooperation => 開封時に連携用URLを呼び出すか
* * date => 受信日時
* * read => 既読状態
*
*/
public function getMessage($request) {
if(is_null($request)) throw new NullPointerException();
if(!array_key_exists('inboxName', $request)) throw new NullPointerException();
if(is_null($request['inboxName'])) throw new NullPointerException();
if(!array_key_exists('messageId', $request)) throw new NullPointerException();
if(is_null($request['messageId'])) throw new NullPointerException();
$query = [];
return $this->doGet(
'Gs2Inbox',
'GetMessage',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox/'. $request['inboxName']. '/message/'. $request['messageId'],
$query);
}
/**
* メッセージを開封<br>
* <br>
* 受信ボックスの設定で開封時自動削除設定が有効な場合は、メッセージは削除されます。<br>
* <br>
* 連携用URLを呼び出す設定になっている場合、連携用URLにメッセージIDを付与したコールバックが実行されます。<br>
* このコールバックをうけて、持ち物を増やしたりすることでメッセージにアイテムを添付することができます。<br>
* <br>
* レスポンスには連携用URLを呼び出した際の応答内容も含まれますので、開封時にさらにメッセージを表示させるようなこともできます。<br>
* 例えば、連携用URLを呼び出した際に「アイテムを手に入れた」というレスポンスを返すことで、このAPIのレスポンスにその文字列も含んだ形で応答されますので、<br>
* 開封時にさらにその応答メッセージを使って画面にメッセージとして「アイテムを手に入れた」という表示をすることができます。<br>
* <br>
* 開封時のコールバックは通信障害などの理由により、コールバック先のサーバは正しく処理を行えたけれど、<br>
* GS2側のインフラにレスポンスが届かず、結果的に処理が失敗する可能性を考慮する必要があります。<br>
* この場合、複数回の開封コールバックが呼び出される可能性がありますので、コールバック処理は冪等性を持った形で実装するようにしてください。<br>
* <br>
* accessToken には {@link http://static.docs.gs2.io/php/auth/class-GS2.Auth.Gs2AuthClient.html#_login Gs2AuthClient::login()} でログインして取得したアクセストークンを指定してください。<br>
*
* @param array $request
* * inboxName => 受信ボックス名
* * messageId => メッセージID
* * accessToken => アクセストークン
* @return array
* * item
* * messageId => メッセージID
* * inboxId => 受信ボックスID
* * userId => 受信ユーザID
* * message => メッセージ本文
* * cooperation => 開封時に連携用URLを呼び出すか
* * date => 受信日時
* * read => 既読状態
*
*/
public function readMessage($request) {
if(is_null($request)) throw new NullPointerException();
if(!array_key_exists('inboxName', $request)) throw new NullPointerException();
if(is_null($request['inboxName'])) throw new NullPointerException();
if(!array_key_exists('messageId', $request)) throw new NullPointerException();
if(is_null($request['messageId'])) throw new NullPointerException();
if(!array_key_exists('accessToken', $request)) throw new NullPointerException();
if(is_null($request['accessToken'])) throw new NullPointerException();
$body = [];
$query = [];
$extparams = [
'headers' => [
'X-GS2-ACCESS-TOKEN' => $request['accessToken']
]
];
return $this->doPost(
'Gs2Inbox',
'ReadMessage',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox/'. $request['inboxName']. '/message/'. $request['messageId'],
$body,
$query,
$extparams);
}
/**
* メッセージを削除<br>
* <br>
* accessToken には {@link http://static.docs.gs2.io/php/auth/class-GS2.Auth.Gs2AuthClient.html#_login Gs2AuthClient::login()} でログインして取得したアクセストークンを指定してください。<br>
*
* @param array $request
* * inboxName => 受信ボックス名
* * messageId => メッセージID
* * accessToken => アクセストークン
*/
public function deleteMessage($request) {
if(is_null($request)) throw new NullPointerException();
if(!array_key_exists('inboxName', $request)) throw new NullPointerException();
if(is_null($request['inboxName'])) throw new NullPointerException();
if(!array_key_exists('messageId', $request)) throw new NullPointerException();
if(is_null($request['messageId'])) throw new NullPointerException();
if(!array_key_exists('accessToken', $request)) throw new NullPointerException();
if(is_null($request['accessToken'])) throw new NullPointerException();
$query = [];
$extparams = [
'headers' => [
'X-GS2-ACCESS-TOKEN' => $request['accessToken']
]
];
return $this->doDelete(
'Gs2Inbox',
'DeleteMessage',
Gs2InboxClient::$ENDPOINT,
Gs2InboxClient::$VARIATION,
'/inbox/'. $request['inboxName']. '/message/'. $request['messageId'],
$query,
$extparams);
}
}