Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| OpsGenieNotifier | |
0.00% |
0 / 34 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| notify | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| buildAlertModel | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
90 | |||
| getAlertApiClient | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\notifiers; |
| 23 | |
| 24 | use GuzzleHttp\Client; |
| 25 | use GuzzleHttp\Exception\GuzzleException; |
| 26 | use JTL\OpsGenie\Client\Alert\Alert; |
| 27 | use JTL\OpsGenie\Client\Alert\CreateAlertRequest; |
| 28 | use JTL\OpsGenie\Client\AlertApiClient; |
| 29 | use JTL\OpsGenie\Client\HttpClient; |
| 30 | use JTL\OpsGenie\Client\Priority; |
| 31 | use JTL\OpsGenie\Client\Responder; |
| 32 | |
| 33 | /** |
| 34 | * Sends a notification to OpsGenie |
| 35 | * |
| 36 | * Class OpsGenieNotifier |
| 37 | * @author Andrey Niahrou <Andrei.Niahrou@1pt.com> |
| 38 | * @package oat\tao\model\notifiers |
| 39 | */ |
| 40 | class OpsGenieNotifier implements NotifierInterface |
| 41 | { |
| 42 | private const OPTION_BASE_URI = 'https://api.eu.opsgenie.com/v2/'; |
| 43 | |
| 44 | /** |
| 45 | * @var string |
| 46 | */ |
| 47 | private $token; |
| 48 | |
| 49 | /** |
| 50 | * @var array |
| 51 | */ |
| 52 | private $parameters; |
| 53 | |
| 54 | public function __construct(string $token, array $parameters = []) |
| 55 | { |
| 56 | $this->token = $token; |
| 57 | $this->parameters = $parameters; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * {@inheritDoc} |
| 62 | * |
| 63 | * @throws GuzzleException |
| 64 | */ |
| 65 | public function notify(string $title, string $description): array |
| 66 | { |
| 67 | $alert = $this->buildAlertModel($title, $description); |
| 68 | $request = new CreateAlertRequest($alert); |
| 69 | $client = $this->getAlertApiClient($this->token); |
| 70 | $response = $client->createAlert($request); |
| 71 | |
| 72 | return [ |
| 73 | 'status' => $response->isSuccessful(), |
| 74 | 'code' => $response->getStatusCode(), |
| 75 | 'message' => $response->getMessage() |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param string $title |
| 81 | * @param string $description |
| 82 | * @return Alert |
| 83 | */ |
| 84 | private function buildAlertModel(string $title, string $description): Alert |
| 85 | { |
| 86 | $alert = new Alert( |
| 87 | isset($this->parameters['entity']) ? $this->parameters['entity'] : '', |
| 88 | isset($this->parameters['alias']) ? $this->parameters['alias'] : '', |
| 89 | $title, |
| 90 | isset($this->parameters['source']) ? $this->parameters['source'] : '', |
| 91 | isset($this->parameters['priority']) ? new Priority($this->parameters['priority']) : Priority::moderate() |
| 92 | ); |
| 93 | |
| 94 | $alert->setDescription($description); |
| 95 | |
| 96 | if (isset($this->parameters['tags'])) { |
| 97 | foreach ($this->parameters['tags'] as $tag) { |
| 98 | $alert->appendTag($tag); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (isset($this->parameters['responders'])) { |
| 103 | foreach ($this->parameters['responders'] as $responder) { |
| 104 | $responderObj = new Responder($responder['id'], $responder['type']); |
| 105 | $alert->appendResponder($responderObj); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return $alert; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param string $authToken |
| 114 | * @return AlertApiClient |
| 115 | */ |
| 116 | private function getAlertApiClient(string $authToken): AlertApiClient |
| 117 | { |
| 118 | $guzzleClient = new Client( |
| 119 | [ |
| 120 | 'base_uri' => self::OPTION_BASE_URI |
| 121 | ] |
| 122 | ); |
| 123 | |
| 124 | return new AlertApiClient(new HttpClient($authToken, $guzzleClient)); |
| 125 | } |
| 126 | } |