| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 | <?phpnamespace phpnotify;use \TypeError as TypeError;use \RuntimeException as RuntimeException;/** * This class is base of the notification. * * This class is base for the notification. It would be enlarged by other * classes with new functions, which gives more functionality. It give only * base, requireed to send notification, that mean content and process for * authentication.  */class notification_base {    /**     * This store request to the server.     * @var fetch     */    private fetch $query;    /**     * This store authorization method which would be used to send     * notification into server.     * @var authorization_method     */    private authorization_method $authorization;    /**     * This create new notification.     *     * This create new notification, it would be used only by library, by      * {@see subject} class. It require url of the subject, content of the     * notificatin and authorization method,     *      * @param string $url Location of the notification subject.     * @param string $content Content of the notification.     * @param authorization_method $method Method of the authorization.     */    public function __construct(        string $url,        string $content,         authorization_method $method    ) {        $this->authorization = $method;        $this->query = new fetch($url);        $this->query->set_method('POST');        $this->query->send_raw($content, 'text/plain');    }    /**     * This function is used to prepare request before send.     *      * This function is called before makes fetch request. It could being     * overwriten, but that function must still being called, to process     * authorization headers.     */    protected function prepare(): void {        if ($this->authorization->is_header_required()) {            $name = $this->authorization->header_name();            $content = $this->authorization->header_content();            $this->query->set_header($name, $content);        }    }      /**     * This function could being used to add functionality of that class.     *     * That makes adding new functionalities to that class simple, because     * add posibility to makes simple one-line functions. That function      * set header of the request, and return notification itself.     *     * @param string $name Name of the header to set.     * @param string $content Content of the header.     *     * @return notification_base Self to chain loading.     */    protected function set(string $name, string $content): object {        $this->query->set_header($name, $content);        return $this;    }    /**     * That function send notification request.     *      * That function send notification request to the notifications server.     * It process request, and return true when all went well. When any error     * occurs, it returns false, or when $throws parameter is true, it throws     * error, which occurs.     *     * @throws RuntimeException When $throws parameter is true end any error     *                          occurs.     *     * @param bool $throws When true function would throw exception on      *                     request error, when false it return false on fetch     *                     error.     *     * @return bool True when notification had been send, false.     */    public function send(bool $throws = false): bool {        try {            $this->prepare();            $result = $this            ->query            ->request()            ->receive_array();            if (array_key_exists('id', $result)) {                return true;            }            if (count($result) === 0) {                throw new RuntimeException('Can not fetch. General error.');            }            if (array_key_exists('error', $result)) {                throw new RuntimeException('"'.$error.'" error occurs.');            }        } catch (Exception $error) {            if ($throws) {                throw $error;            }            return false;        }    }}
 |