Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
18 / 19 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| JsonWebhookPayloadFactory | |
94.74% |
18 / 19 |
|
66.67% |
2 / 3 |
5.00 | |
0.00% |
0 / 1 |
| createPayload | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
2.00 | |||
| getContentType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSourceUrl | |
100.00% |
3 / 3 |
|
100.00% |
1 / 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) 2019 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\webhooks\task; |
| 22 | |
| 23 | use oat\oatbox\service\ConfigurableService; |
| 24 | |
| 25 | class JsonWebhookPayloadFactory extends ConfigurableService implements WebhookPayloadFactoryInterface |
| 26 | { |
| 27 | public const SOURCE = 'source'; |
| 28 | public const EVENTS = 'events'; |
| 29 | |
| 30 | public const EVENT_ID = 'eventId'; |
| 31 | public const EVENT_NAME = 'eventName'; |
| 32 | public const TRIGGERED_TIMESTAMP = 'triggeredTimestamp'; |
| 33 | public const EVENT_DATA = 'eventData'; |
| 34 | |
| 35 | /** |
| 36 | * @param string $eventName |
| 37 | * @param string $eventId |
| 38 | * @param int $triggeredTimestamp |
| 39 | * @param array $eventData |
| 40 | * @return string |
| 41 | * @throws \common_Exception |
| 42 | */ |
| 43 | public function createPayload($eventName, $eventId, $triggeredTimestamp, $eventData) |
| 44 | { |
| 45 | $data = [ |
| 46 | self::SOURCE => $this->getSourceUrl(), |
| 47 | self::EVENTS => [ |
| 48 | [ |
| 49 | self::EVENT_ID => $eventId, |
| 50 | self::EVENT_NAME => $eventName, |
| 51 | self::TRIGGERED_TIMESTAMP => $triggeredTimestamp, |
| 52 | self::EVENT_DATA => $eventData |
| 53 | ] |
| 54 | ] |
| 55 | ]; |
| 56 | |
| 57 | $result = json_encode($data); |
| 58 | if ($result === false) { |
| 59 | throw new \common_Exception("Can't prepare payload: " . json_last_error_msg()); |
| 60 | } |
| 61 | |
| 62 | return $result; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return string |
| 67 | */ |
| 68 | public function getContentType() |
| 69 | { |
| 70 | return 'application/json'; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return string |
| 75 | */ |
| 76 | private function getSourceUrl() |
| 77 | { |
| 78 | return defined('ROOT_URL') |
| 79 | ? ROOT_URL |
| 80 | : gethostname(); |
| 81 | } |
| 82 | } |