notification_base.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace phpnotify;
  3. use \TypeError as TypeError;
  4. use \RuntimeException as RuntimeException;
  5. /**
  6. * This class is base of the notification.
  7. *
  8. * This class is base for the notification. It would be enlarged by other
  9. * classes with new functions, which gives more functionality. It give only
  10. * base, requireed to send notification, that mean content and process for
  11. * authentication.
  12. */
  13. class notification_base {
  14. /**
  15. * This store request to the server.
  16. * @var fetch
  17. */
  18. private fetch $query;
  19. /**
  20. * This store authorization method which would be used to send
  21. * notification into server.
  22. * @var authorization_method
  23. */
  24. private authorization_method $authorization;
  25. /**
  26. * This create new notification.
  27. *
  28. * This create new notification, it would be used only by library, by
  29. * {@see subject} class. It require url of the subject, content of the
  30. * notificatin and authorization method,
  31. *
  32. * @param string $url Location of the notification subject.
  33. * @param string $content Content of the notification.
  34. * @param authorization_method $method Method of the authorization.
  35. */
  36. public function __construct(
  37. string $url,
  38. string $content,
  39. authorization_method $method
  40. ) {
  41. $this->authorization = $method;
  42. $this->query = new fetch($url);
  43. $this->query->set_method('POST');
  44. $this->query->send_raw($content, 'text/plain');
  45. }
  46. /**
  47. * This function is used to prepare request before send.
  48. *
  49. * This function is called before makes fetch request. It could being
  50. * overwriten, but that function must still being called, to process
  51. * authorization headers.
  52. */
  53. protected function prepare(): void {
  54. if ($this->authorization->is_header_required()) {
  55. $name = $this->authorization->header_name();
  56. $content = $this->authorization->header_content();
  57. $this->query->set_header($name, $content);
  58. }
  59. }
  60. /**
  61. * This function could being used to add functionality of that class.
  62. *
  63. * That makes adding new functionalities to that class simple, because
  64. * add posibility to makes simple one-line functions. That function
  65. * set header of the request, and return notification itself.
  66. *
  67. * @param string $name Name of the header to set.
  68. * @param string $content Content of the header.
  69. *
  70. * @return notification_base Self to chain loading.
  71. */
  72. protected function set(string $name, string $content): object {
  73. $this->query->set_header($name, $content);
  74. return $this;
  75. }
  76. /**
  77. * That function send notification request.
  78. *
  79. * That function send notification request to the notifications server.
  80. * It process request, and return true when all went well. When any error
  81. * occurs, it returns false, or when $throws parameter is true, it throws
  82. * error, which occurs.
  83. *
  84. * @throws RuntimeException When $throws parameter is true end any error
  85. * occurs.
  86. *
  87. * @param bool $throws When true function would throw exception on
  88. * request error, when false it return false on fetch
  89. * error.
  90. *
  91. * @return bool True when notification had been send, false.
  92. */
  93. public function send(bool $throws = false): bool {
  94. try {
  95. $this->prepare();
  96. $result = $this
  97. ->query
  98. ->request()
  99. ->receive_array();
  100. if (array_key_exists('id', $result)) {
  101. return true;
  102. }
  103. if (count($result) === 0) {
  104. throw new RuntimeException('Can not fetch. General error.');
  105. }
  106. if (array_key_exists('error', $result)) {
  107. throw new RuntimeException('"'.$error.'" error occurs.');
  108. }
  109. } catch (Exception $error) {
  110. if ($throws) {
  111. throw $error;
  112. }
  113. return false;
  114. }
  115. }
  116. }