Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResourceUrlBuilder
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
156
0.00% covered (danger)
0.00%
0 / 1
 buildUrl
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
 getBackofficeUrl
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 isSectionApplicable
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
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
23declare(strict_types=1);
24
25namespace oat\taoBackOffice\model\routing;
26
27use LogicException;
28use oat\tao\model\menu\Tree;
29use InvalidArgumentException;
30use core_kernel_classes_Class;
31use oat\tao\model\menu\Section;
32use core_kernel_classes_Resource;
33use oat\tao\model\menu\MenuService;
34use oat\tao\model\menu\Perspective;
35use oat\oatbox\service\ConfigurableService;
36
37class 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            'section' => $section->getId(),
89            'uri' => $resource->getUri(),
90        ]);
91    }
92
93    private function isSectionApplicable(core_kernel_classes_Class $resourceClass, Section $section): bool
94    {
95        /** @var Tree $tree */
96        foreach ($section->getTrees() as $tree) {
97            $rootClass = $resourceClass->getClass($tree->get('rootNode'));
98
99            if ($rootClass->equals($resourceClass) || $resourceClass->isSubClassOf($rootClass)) {
100                return true;
101            }
102        }
103
104        return false;
105    }
106}