notifier.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace phpnotify;
  3. use \TypeError as TypeError;
  4. /**
  5. * This represents notification server, in library named notifier.
  6. *
  7. * This class represents notification server in the system. It is required
  8. * to subscribe new subject. It also store auth service like login and
  9. * password, or auth token. That class would be used to generate new subjects
  10. * with same server url, and potentially same authorization methods. It makes
  11. * generating that subjects much easier, without passing same authentication
  12. * and server URL to all new subjects. Change authentication method in new
  13. * subject generated by notifier would not change notifier authentication
  14. * method.
  15. */
  16. class notifier extends authorization_store {
  17. /**
  18. * This store URL of the notifications server.
  19. * @var string
  20. */
  21. private string $url;
  22. /**
  23. * This function initialize new notifier by the server URL.
  24. *
  25. * This create new notifications server wrapper, from that URL. It
  26. * use empty authorization as default authorization method. URL is
  27. * validating, and when incorrect URL format had been incorrect throws
  28. * TypeError.
  29. *
  30. * @throws TypeError When URL is bad formated.
  31. *
  32. * @param string URL of the server.
  33. */
  34. public function __construct(string $url) {
  35. parent::__construct();
  36. if (!filter_var($url, FILTER_VALIDATE_URL)) {
  37. throw new TypeError('"'.$url.'" is not property URL.');
  38. }
  39. $this->url = $url;
  40. }
  41. /**
  42. * This function create new notifier from URL.
  43. *
  44. * That function create new notifier from URL, but it not use 'new'
  45. * keyword, which results in the better look code with chainloading.
  46. * It use same constructor as {@see notifier::__construct}.
  47. *
  48. * @throws TypeError Whem URL is bad formated.
  49. *
  50. * @param string $url URL for the notifications server.
  51. * @return notifier New notification server.
  52. */
  53. public static function create(string $url): object {
  54. return new self($url);
  55. }
  56. /**
  57. * This function would be use to select a subject.
  58. *
  59. * This function select a subject to notify in. It return new subject
  60. * which is initialized from notifier URL and authorization method.
  61. * When authorization method of the subject had been changed, notifier
  62. * still use own authorization method for new subjects.
  63. *
  64. * @param string $subject Name of the subject to use.
  65. *
  66. * @return subject New subject to send notifications in.
  67. */
  68. public function topic(string $subject): subject {
  69. return new subject(
  70. $this->url,
  71. $subject,
  72. $this->get_access_method()
  73. );
  74. }
  75. }