SchedulePayload.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. class SchedulePayload {
  3. private static $LIMIT_KEYS = array('X-Rate-Limit-Limit'=>'rateLimitLimit', 'X-Rate-Limit-Remaining'=>'rateLimitRemaining', 'X-Rate-Limit-Reset'=>'rateLimitReset');
  4. const SCHEDULES_URL = 'https://api.jpush.cn/v3/schedules';
  5. private $client;
  6. /**
  7. * SchedulePayload constructor.
  8. * @param $client JPush
  9. */
  10. public function __construct($client) {
  11. $this->client = $client;
  12. }
  13. public function createSingleSchedule($name, $push_payload, $trigger) {
  14. if (!is_string($name)) {
  15. throw new InvalidArgumentException('Invalid schedule name');
  16. }
  17. if (!is_array($push_payload)) {
  18. throw new InvalidArgumentException('Invalid schedule push payload');
  19. }
  20. if (!is_array($trigger)) {
  21. throw new InvalidArgumentException('Invalid schedule trigger');
  22. }
  23. $payload = array();
  24. $payload['name'] = $name;
  25. $payload['enabled'] = true;
  26. $payload['trigger'] = array("single"=>$trigger);
  27. $payload['push'] = $push_payload;
  28. $response = $this->client->_request(SchedulePayload::SCHEDULES_URL, JPush::HTTP_POST, json_encode($payload));
  29. return $this->__processResp($response);
  30. }
  31. public function createPeriodicalSchedule($name, $push_payload, $trigger) {
  32. if (!is_string($name)) {
  33. throw new InvalidArgumentException('Invalid schedule name');
  34. }
  35. if (!is_array($push_payload)) {
  36. throw new InvalidArgumentException('Invalid schedule push payload');
  37. }
  38. if (!is_array($trigger)) {
  39. throw new InvalidArgumentException('Invalid schedule trigger');
  40. }
  41. $payload = array();
  42. $payload['name'] = $name;
  43. $payload['enabled'] = true;
  44. $payload['trigger'] = array("periodical"=>$trigger);
  45. $payload['push'] = $push_payload;
  46. $response = $this->client->_request(SchedulePayload::SCHEDULES_URL, JPush::HTTP_POST, json_encode($payload));
  47. return $this->__processResp($response);
  48. }
  49. public function updateSingleSchedule($schedule_id, $name=null, $enabled=null, $push_payload=null, $trigger=null) {
  50. if (!is_string($schedule_id)) {
  51. throw new InvalidArgumentException('Invalid schedule id');
  52. }
  53. $payload = array();
  54. if (!is_null($name)) {
  55. if (!is_string($name)) {
  56. throw new InvalidArgumentException('Invalid schedule name');
  57. } else {
  58. $payload['name'] = $name;
  59. }
  60. }
  61. if (!is_null($enabled)) {
  62. if (!is_bool($enabled)) {
  63. throw new InvalidArgumentException('Invalid schedule enable');
  64. } else {
  65. $payload['enabled'] = $enabled;
  66. }
  67. }
  68. if (!is_null($push_payload)) {
  69. if (!is_array($push_payload)) {
  70. throw new InvalidArgumentException('Invalid schedule push payload');
  71. } else {
  72. $payload['push'] = $push_payload;
  73. }
  74. }
  75. if (!is_null($trigger)) {
  76. if (!is_array($trigger)) {
  77. throw new InvalidArgumentException('Invalid schedule trigger');
  78. } else {
  79. $payload['trigger'] = array("single"=>$trigger);
  80. }
  81. }
  82. if (count($payload) <= 0) {
  83. throw new InvalidArgumentException('Invalid schedule, name, enabled, trigger, push can not all be null');
  84. }
  85. $url = SchedulePayload::SCHEDULES_URL . "/" . $schedule_id;
  86. $response = $this->client->_request($url, JPush::HTTP_PUT, json_encode($payload));
  87. return $this->__processResp($response);
  88. }
  89. public function updatePeriodicalSchedule($schedule_id, $name=null, $enabled=null, $push_payload=null, $trigger=null) {
  90. if (!is_string($schedule_id)) {
  91. throw new InvalidArgumentException('Invalid schedule id');
  92. }
  93. $payload = array();
  94. if (!is_null($name)) {
  95. if (!is_string($name)) {
  96. throw new InvalidArgumentException('Invalid schedule name');
  97. } else {
  98. $payload['name'] = $name;
  99. }
  100. }
  101. if (!is_null($enabled)) {
  102. if (!is_bool($enabled)) {
  103. throw new InvalidArgumentException('Invalid schedule enable');
  104. } else {
  105. $payload['enabled'] = $enabled;
  106. }
  107. }
  108. if (!is_null($push_payload)) {
  109. if (!is_array($push_payload)) {
  110. throw new InvalidArgumentException('Invalid schedule push payload');
  111. } else {
  112. $payload['push'] = $push_payload;
  113. }
  114. }
  115. if (!is_null($trigger)) {
  116. if (!is_array($trigger)) {
  117. throw new InvalidArgumentException('Invalid schedule trigger');
  118. } else {
  119. $payload['trigger'] = array("periodical"=>$trigger);
  120. }
  121. }
  122. if (count($payload) <= 0) {
  123. throw new InvalidArgumentException('Invalid schedule, name, enabled, trigger, push can not all be null');
  124. }
  125. $url = SchedulePayload::SCHEDULES_URL . "/" . $schedule_id;
  126. $response = $this->client->_request($url, JPush::HTTP_PUT, json_encode($payload));
  127. return $this->__processResp($response);
  128. }
  129. public function getSchedules($page=1) {
  130. if (!is_int($page)) {
  131. throw new InvalidArgumentException('Invalid pages');
  132. }
  133. $url = SchedulePayload::SCHEDULES_URL . "?page=" . $page;
  134. $response = $this->client->_request($url, JPush::HTTP_GET);
  135. return $this->__processResp($response);
  136. }
  137. public function getSchedule($schedule_id) {
  138. if (!is_string($schedule_id)) {
  139. throw new InvalidArgumentException('Invalid schedule id');
  140. }
  141. $url = SchedulePayload::SCHEDULES_URL . "/" . $schedule_id;
  142. $response = $this->client->_request($url, JPush::HTTP_GET);
  143. return $this->__processResp($response);
  144. }
  145. public function deleteSchedule($schedule_id) {
  146. if (!is_string($schedule_id)) {
  147. throw new InvalidArgumentException('Invalid schedule id');
  148. }
  149. $url = SchedulePayload::SCHEDULES_URL . "/" . $schedule_id;
  150. $response = $this->client->_request($url, JPush::HTTP_DELETE);
  151. return $this->__processResp($response);
  152. }
  153. private function __processResp($response) {
  154. if($response['http_code'] === 200) {
  155. $body = array();
  156. $data = json_decode($response['body']);
  157. if (!is_null($data)) {
  158. $body['data'] = json_decode($response['body']);
  159. }
  160. $headers = $response['headers'];
  161. if (is_array($headers)) {
  162. $limit = array();
  163. foreach (self::$LIMIT_KEYS as $key => $value) {
  164. if (array_key_exists($key, $headers)) {
  165. $limit[$value] = $headers[$key];
  166. }
  167. }
  168. if (count($limit) > 0) {
  169. $body['limit'] = (object)$limit;
  170. }
  171. return (object)$body;
  172. }
  173. return $body;
  174. } else {
  175. throw new APIRequestException($response);
  176. }
  177. }
  178. }