Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RootClassesListService
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 list
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 listUris
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 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) 2021 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\model\resources\Service;
24
25use oat\tao\model\menu\Tree;
26use core_kernel_classes_Class;
27use oat\tao\model\menu\Section;
28use oat\tao\model\menu\MenuService;
29use oat\tao\model\menu\Perspective;
30use oat\generis\model\data\Ontology;
31use oat\tao\model\resources\Contract\RootClassesListServiceInterface;
32
33class RootClassesListService implements RootClassesListServiceInterface
34{
35    /** @var Ontology */
36    private $ontology;
37
38    /** @var Perspective[] */
39    private $perspectives;
40
41    /** @var array<string, string[]|core_kernel_classes_Class[]> */
42    private $cache = [];
43
44    /**
45     * @param Perspective[]|null $perspectives
46     */
47    public function __construct(Ontology $ontology, array $perspectives = null)
48    {
49        $this->ontology = $ontology;
50        $this->perspectives = $perspectives ?? MenuService::getAllPerspectives();
51    }
52
53    /**
54     * {@inheritdoc}
55     */
56    public function list(): array
57    {
58        if (!isset($this->cache[__FUNCTION__])) {
59            $rootClasses = [];
60
61            foreach ($this->listUris() as $rootClassUri) {
62                $rootClasses[] = $this->ontology->getClass($rootClassUri);
63            }
64
65            $this->cache[__FUNCTION__] = $rootClasses;
66        }
67
68        return $this->cache[__FUNCTION__];
69    }
70
71    /**
72     * {@inheritdoc}
73     */
74    public function listUris(): array
75    {
76        if (!isset($this->cache[__FUNCTION__])) {
77            $rootClassesUris = [];
78
79            foreach ($this->perspectives as $perspective) {
80                /** @var Section $structure */
81                foreach ($perspective->getChildren() as $structure) {
82                    /** @var Tree $tree */
83                    foreach ($structure->getTrees() as $tree) {
84                        $rootClassUri = $tree->get('rootNode');
85
86                        if (!empty($rootClassUri)) {
87                            $rootClassesUris[] = $rootClassUri;
88                        }
89                    }
90                }
91            }
92
93            $this->cache[__FUNCTION__] = $rootClassesUris;
94        }
95
96        return $this->cache[__FUNCTION__];
97    }
98}