Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
12 / 14 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ThemeAutoSetListener | |
85.71% |
12 / 14 |
|
33.33% |
1 / 3 |
7.14 | |
0.00% |
0 / 1 |
| whenDeliveryIsCreated | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
4.02 | |||
| getThemeDiscoverService | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| getAutoSetThemeService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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\taoDeliveryRdf\model\theme\Listener; |
| 24 | |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\taoDeliveryRdf\model\event\DeliveryCreatedEvent; |
| 27 | use oat\taoDeliveryRdf\model\theme\Exception\ThemeAutoSetNotSupported; |
| 28 | use oat\taoDeliveryRdf\model\theme\Service\ThemeAutoSetService; |
| 29 | use oat\taoDeliveryRdf\model\theme\Service\ThemeDiscoverServiceInterface; |
| 30 | use Throwable; |
| 31 | |
| 32 | class ThemeAutoSetListener extends ConfigurableService |
| 33 | { |
| 34 | public const SERVICE_ID = 'taoDeliveryRdf/ThemeAutoSetListener'; |
| 35 | public const OPTION_THEME_DISCOVER_SERVICE = 'themeDiscoverService'; |
| 36 | |
| 37 | public function whenDeliveryIsCreated(DeliveryCreatedEvent $event): void |
| 38 | { |
| 39 | try { |
| 40 | $autoSetThemeService = $this->getAutoSetThemeService(); |
| 41 | $themeDiscoverService = $this->getThemeDiscoverService(); |
| 42 | |
| 43 | if ($themeDiscoverService) { |
| 44 | $autoSetThemeService->setThemeDiscoverService($themeDiscoverService); |
| 45 | } |
| 46 | |
| 47 | $autoSetThemeService->setThemeByDelivery($event->getDeliveryUri()); |
| 48 | } catch (ThemeAutoSetNotSupported $exception) { |
| 49 | $this->logInfo(sprintf('AutoSet Theme not supported: %s', $exception->getMessage())); |
| 50 | } catch (Throwable $exception) { |
| 51 | $this->logError(sprintf('Could not set theme: %s', $exception->getMessage())); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private function getThemeDiscoverService(): ?ThemeDiscoverServiceInterface |
| 56 | { |
| 57 | $themeDiscoverServiceId = $this->getOption(self::OPTION_THEME_DISCOVER_SERVICE); |
| 58 | |
| 59 | return $themeDiscoverServiceId |
| 60 | ? $this->getServiceManager()->getContainer()->get($themeDiscoverServiceId) |
| 61 | : null; |
| 62 | } |
| 63 | |
| 64 | private function getAutoSetThemeService(): ThemeAutoSetService |
| 65 | { |
| 66 | return $this->getServiceManager()->getContainer()->get(ThemeAutoSetService::class); |
| 67 | } |
| 68 | } |