Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.00% |
51 / 60 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ThemeAutoSetService | |
85.00% |
51 / 60 |
|
40.00% |
2 / 5 |
12.49 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| setThemeDiscoverService | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setThemeByDelivery | |
96.15% |
25 / 26 |
|
0.00% |
0 / 1 |
4 | |||
| discoverThemeId | |
76.92% |
20 / 26 |
|
0.00% |
0 / 1 |
4.20 | |||
| getOriginDeliveryId | |
100.00% |
2 / 2 |
|
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) 2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoDeliveryRdf\model\theme\Service; |
| 24 | |
| 25 | use core_kernel_classes_Resource; |
| 26 | use oat\generis\model\data\Ontology; |
| 27 | use oat\tao\model\theme\ThemeServiceAbstract; |
| 28 | use oat\taoDeliveryRdf\model\theme\DeliveryThemeDetailsProvider; |
| 29 | use oat\taoDeliveryRdf\model\theme\Exception\ThemeAutoSetNotSupported; |
| 30 | use oat\taoQtiTest\model\Domain\Model\QtiTestRepositoryInterface; |
| 31 | |
| 32 | class ThemeAutoSetService |
| 33 | { |
| 34 | private const PUBLISHING_ORIGINAL_ID = 'http://www.tao.lu/Ontologies/TAOPublisher.rdf#OriginDeliveryID'; |
| 35 | |
| 36 | /** @var Ontology */ |
| 37 | private $ontology; |
| 38 | |
| 39 | /** @var DeliveryThemeDetailsProvider */ |
| 40 | private $deliveryThemeDetailsProvider; |
| 41 | |
| 42 | /** @var QtiTestRepositoryInterface */ |
| 43 | private $qtiTestRepository; |
| 44 | |
| 45 | /** @var ThemeServiceAbstract */ |
| 46 | private $themeService; |
| 47 | |
| 48 | /** @var ThemeDiscoverServiceInterface */ |
| 49 | private $themeDiscoverService; |
| 50 | |
| 51 | public function __construct( |
| 52 | Ontology $ontology, |
| 53 | DeliveryThemeDetailsProvider $deliveryThemeDetailsProvider, |
| 54 | QtiTestRepositoryInterface $qtiTestRepository, |
| 55 | ThemeServiceAbstract $themeService |
| 56 | ) { |
| 57 | $this->ontology = $ontology; |
| 58 | $this->deliveryThemeDetailsProvider = $deliveryThemeDetailsProvider; |
| 59 | $this->qtiTestRepository = $qtiTestRepository; |
| 60 | $this->themeService = $themeService; |
| 61 | } |
| 62 | |
| 63 | public function setThemeDiscoverService(ThemeDiscoverServiceInterface $themeDiscoverService): self |
| 64 | { |
| 65 | $this->themeDiscoverService = $themeDiscoverService; |
| 66 | |
| 67 | return $this; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @throws ThemeAutoSetNotSupported |
| 72 | */ |
| 73 | public function setThemeByDelivery(string $deliveryUri): void |
| 74 | { |
| 75 | $delivery = $this->ontology->getResource($deliveryUri); |
| 76 | $originDeliveryId = $this->getOriginDeliveryId($delivery); |
| 77 | |
| 78 | if (!empty($originDeliveryId)) { |
| 79 | throw new ThemeAutoSetNotSupported( |
| 80 | sprintf( |
| 81 | 'Cannot auto-set theme for remote publishing [delivery:%s, original: %s]', |
| 82 | $deliveryUri, |
| 83 | $originDeliveryId |
| 84 | ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | $currentThemeId = $this->deliveryThemeDetailsProvider->getDeliveryThemeIdFromDb($deliveryUri); |
| 89 | |
| 90 | if (!empty($currentThemeId)) { |
| 91 | throw new ThemeAutoSetNotSupported( |
| 92 | sprintf( |
| 93 | 'Cannot auto-set theme cause theme "%s" is already set for delivery "%s"', |
| 94 | $currentThemeId, |
| 95 | $deliveryUri |
| 96 | ) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | $themeId = isset($this->themeDiscoverService) |
| 101 | ? $this->themeDiscoverService->discoverByDelivery($deliveryUri) |
| 102 | : $this->discoverThemeId($deliveryUri); |
| 103 | |
| 104 | $delivery->setPropertyValue( |
| 105 | $delivery->getProperty(DeliveryThemeDetailsProvider::DELIVERY_THEME_ID_URI), |
| 106 | $themeId |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | private function discoverThemeId(string $deliveryUri): string |
| 111 | { |
| 112 | $test = $this->qtiTestRepository->findByDelivery($deliveryUri); |
| 113 | |
| 114 | if (!$test) { |
| 115 | throw new ThemeAutoSetNotSupported( |
| 116 | sprintf( |
| 117 | 'Cannot auto-set theme cause delivery "%s" does not have a test', |
| 118 | $deliveryUri |
| 119 | ) |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | $language = $test->getLanguage(); |
| 124 | |
| 125 | if (empty($language)) { |
| 126 | throw new ThemeAutoSetNotSupported( |
| 127 | sprintf( |
| 128 | 'Cannot auto-set theme cause test "%s" does not have a language', |
| 129 | $test->getUri() |
| 130 | ) |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | $themeId = $this->themeService->getFirstThemeIdByLanguage($language) |
| 135 | ?? $this->themeService->getCurrentThemeId(); |
| 136 | |
| 137 | if ($themeId === null) { |
| 138 | throw new ThemeAutoSetNotSupported( |
| 139 | sprintf( |
| 140 | 'Cannot auto-set theme cause there is not theme associated for language "%s"', |
| 141 | $language |
| 142 | ) |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | return $themeId; |
| 147 | } |
| 148 | |
| 149 | private function getOriginDeliveryId(core_kernel_classes_Resource $delivery): string |
| 150 | { |
| 151 | $propertyValue = $delivery->getOnePropertyValue($delivery->getProperty(self::PUBLISHING_ORIGINAL_ID)); |
| 152 | |
| 153 | return $propertyValue ? $propertyValue->getUri() : ''; |
| 154 | } |
| 155 | } |