Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ResourceUrlBuilder | |
0.00% |
0 / 26 |
|
0.00% |
0 / 3 |
156 | |
0.00% |
0 / 1 |
| buildUrl | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
56 | |||
| getBackofficeUrl | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| isSectionApplicable | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
| 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) 2018-2022 (original work) Open Assessment Technologies SA. |
| 19 | * |
| 20 | * @author Gyula Szucs <gyula@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\taoBackOffice\model\routing; |
| 26 | |
| 27 | use LogicException; |
| 28 | use oat\tao\model\menu\Tree; |
| 29 | use InvalidArgumentException; |
| 30 | use core_kernel_classes_Class; |
| 31 | use oat\tao\model\menu\Section; |
| 32 | use core_kernel_classes_Resource; |
| 33 | use oat\tao\model\menu\MenuService; |
| 34 | use oat\tao\model\menu\Perspective; |
| 35 | use oat\oatbox\service\ConfigurableService; |
| 36 | |
| 37 | class ResourceUrlBuilder extends ConfigurableService |
| 38 | { |
| 39 | public const SERVICE_ID = 'taoBackOffice/resourceUrlBuilder'; |
| 40 | |
| 41 | /** |
| 42 | * Builds a full URL for a resource |
| 43 | * |
| 44 | * @return string |
| 45 | */ |
| 46 | public function buildUrl(core_kernel_classes_Resource $resource) |
| 47 | { |
| 48 | $resourceClass = $resource->getClass($resource); |
| 49 | |
| 50 | if (!$resource->exists() && !$resourceClass->exists()) { |
| 51 | throw new InvalidArgumentException('The requested resource does not exist or has been deleted'); |
| 52 | } |
| 53 | |
| 54 | if (!$resource->isClass()) { |
| 55 | $resourceClass = array_values($resource->getTypes())[0]; |
| 56 | } |
| 57 | |
| 58 | /** @var Perspective $perspective */ |
| 59 | foreach (MenuService::getAllPerspectives() as $perspective) { |
| 60 | /** @var Section $section */ |
| 61 | foreach ($perspective->getChildren() as $section) { |
| 62 | if ($this->isSectionApplicable($resourceClass, $section)) { |
| 63 | return $this->getBackofficeUrl($perspective, $section, $resource); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | throw new LogicException( |
| 69 | sprintf( |
| 70 | 'No url could be built for "%s"', |
| 71 | $resource->getUri() |
| 72 | ) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Generates the actual URL based on perspective and section |
| 78 | * |
| 79 | * @return string |
| 80 | */ |
| 81 | private function getBackofficeUrl( |
| 82 | Perspective $perspective, |
| 83 | Section $section, |
| 84 | core_kernel_classes_Resource $resource |
| 85 | ) { |
| 86 | return _url('index', 'Main', 'tao', [ |
| 87 | 'structure' => $perspective->getId(), |
| 88 | 'ext' => $perspective->getExtension(), |
| 89 | 'section' => $section->getId(), |
| 90 | 'uri' => $resource->getUri(), |
| 91 | ]); |
| 92 | } |
| 93 | |
| 94 | private function isSectionApplicable(core_kernel_classes_Class $resourceClass, Section $section): bool |
| 95 | { |
| 96 | /** @var Tree $tree */ |
| 97 | foreach ($section->getTrees() as $tree) { |
| 98 | $rootClass = $resourceClass->getClass($tree->get('rootNode')); |
| 99 | |
| 100 | if ($rootClass->equals($resourceClass) || $resourceClass->isSubClassOf($rootClass)) { |
| 101 | return true; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return false; |
| 106 | } |
| 107 | } |