Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| GrafanaNotifier | |
0.00% |
0 / 27 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| notify | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| buildPayload | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| 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) 2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\notifiers; |
| 24 | |
| 25 | use GuzzleHttp\Client; |
| 26 | use GuzzleHttp\Exception\GuzzleException; |
| 27 | use GuzzleHttp\RequestOptions; |
| 28 | use Ramsey\Uuid\Uuid; |
| 29 | |
| 30 | class GrafanaNotifier implements NotifierInterface |
| 31 | { |
| 32 | /** @var string */ |
| 33 | private $notificationEndpoint; |
| 34 | /** @var array */ |
| 35 | private $parameters; |
| 36 | |
| 37 | /** |
| 38 | * @param string $notificationEndpoint - endpoint for notification specified in Grafana integration |
| 39 | * @param array $parameters - list of parameters can be configured specifically for application stack |
| 40 | */ |
| 41 | public function __construct(string $notificationEndpoint, array $parameters = []) |
| 42 | { |
| 43 | $this->notificationEndpoint = $notificationEndpoint; |
| 44 | $this->parameters = $parameters; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @inheritDoc |
| 49 | */ |
| 50 | public function notify(string $title, string $description): array |
| 51 | { |
| 52 | $client = new Client(); |
| 53 | try { |
| 54 | $response = $client->request('POST', $this->notificationEndpoint, [ |
| 55 | RequestOptions::JSON => $this->buildPayload($title, $description) |
| 56 | ]); |
| 57 | return [ |
| 58 | 'status' => true, |
| 59 | 'code' => $response->getStatusCode(), |
| 60 | 'message' => $response->getBody()->getContents() |
| 61 | ]; |
| 62 | } catch (GuzzleException $e) { |
| 63 | return [ |
| 64 | 'status' => false, |
| 65 | 'code' => $e->getCode(), |
| 66 | 'message' => $e->getMessage() |
| 67 | ]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private function buildPayload(string $title, string $description): array |
| 72 | { |
| 73 | $payload = [ |
| 74 | 'alert_uuid' => Uuid::uuid4()->toString(), |
| 75 | 'title' => $title, |
| 76 | 'message' => $description, |
| 77 | 'source' => defined('ROOT_URL') ? ROOT_URL : null |
| 78 | ]; |
| 79 | |
| 80 | |
| 81 | if (count($this->parameters) > 0) { |
| 82 | $payload['extra'] = $this->parameters; |
| 83 | } |
| 84 | |
| 85 | return $payload; |
| 86 | } |
| 87 | } |