Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| PubSubObserver | |
0.00% |
0 / 22 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| update | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
6 | |||
| 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\Observer\GCP; |
| 24 | |
| 25 | use ErrorException; |
| 26 | use Google\Cloud\PubSub\PubSubClient; |
| 27 | use Psr\Log\LoggerInterface; |
| 28 | use SplObserver; |
| 29 | use SplSubject; |
| 30 | |
| 31 | class PubSubObserver implements SplObserver |
| 32 | { |
| 33 | public const CONFIG_TOPIC = 'config_topic'; |
| 34 | |
| 35 | /** @var string[] */ |
| 36 | private $config; |
| 37 | |
| 38 | /** @var PubSubClient */ |
| 39 | private $pubSubClient; |
| 40 | |
| 41 | /** @var LoggerInterface */ |
| 42 | private $logger; |
| 43 | |
| 44 | public function __construct(PubSubClient $pubSubClient, LoggerInterface $logger, array $config = []) |
| 45 | { |
| 46 | $this->pubSubClient = $pubSubClient; |
| 47 | $this->logger = $logger; |
| 48 | $this->config = $config; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @inheritDoc |
| 53 | */ |
| 54 | public function update(SplSubject $subject): void |
| 55 | { |
| 56 | if (!isset($this->config[self::CONFIG_TOPIC])) { |
| 57 | throw new ErrorException(sprintf('Config "%s" is missing', self::CONFIG_TOPIC)); |
| 58 | } |
| 59 | |
| 60 | $messageIds = $this->pubSubClient |
| 61 | ->topic($this->config[self::CONFIG_TOPIC]) |
| 62 | ->publish( |
| 63 | [ |
| 64 | 'data' => json_encode($subject), |
| 65 | ] |
| 66 | ); |
| 67 | |
| 68 | $this->logger->info( |
| 69 | sprintf( |
| 70 | 'Pub/Sub observer updated for topic "%s", subject "%s", data "%s", messages "%s"', |
| 71 | $this->config[self::CONFIG_TOPIC], |
| 72 | get_class($subject), |
| 73 | substr((string)json_encode($subject), 0, 250) . '...', |
| 74 | implode(',', $messageIds['messageIds'] ?? []) |
| 75 | ) |
| 76 | ); |
| 77 | } |
| 78 | } |