* @package oat\tao\model\notifiers */ class OpsGenieNotifier implements NotifierInterface { private const OPTION_BASE_URI = 'https://api.eu.opsgenie.com/v2/'; /** * @var string */ private $token; /** * @var array */ private $parameters; public function __construct(string $token, array $parameters = []) { $this->token = $token; $this->parameters = $parameters; } /** * {@inheritDoc} * * @throws GuzzleException */ public function notify(string $title, string $description): array { $alert = $this->buildAlertModel($title, $description); $request = new CreateAlertRequest($alert); $client = $this->getAlertApiClient($this->token); $response = $client->createAlert($request); return [ 'status' => $response->isSuccessful(), 'code' => $response->getStatusCode(), 'message' => $response->getMessage() ]; } /** * @param string $title * @param string $description * @return Alert */ private function buildAlertModel(string $title, string $description): Alert { $alert = new Alert( isset($this->parameters['entity']) ? $this->parameters['entity'] : '', isset($this->parameters['alias']) ? $this->parameters['alias'] : '', $title, isset($this->parameters['source']) ? $this->parameters['source'] : '', isset($this->parameters['priority']) ? new Priority($this->parameters['priority']) : Priority::moderate() ); $alert->setDescription($description); if (isset($this->parameters['tags'])) { foreach ($this->parameters['tags'] as $tag) { $alert->appendTag($tag); } } if (isset($this->parameters['responders'])) { foreach ($this->parameters['responders'] as $responder) { $responderObj = new Responder($responder['id'], $responder['type']); $alert->appendResponder($responderObj); } } return $alert; } /** * @param string $authToken * @return AlertApiClient */ private function getAlertApiClient(string $authToken): AlertApiClient { $guzzleClient = new Client( [ 'base_uri' => self::OPTION_BASE_URI ] ); return new AlertApiClient(new HttpClient($authToken, $guzzleClient)); } }