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; } } }