Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
IndexService
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 6
156
0.00% covered (danger)
0.00%
0 / 1
 runIndexing
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getDocumentBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createDocumentFromResource
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 createDocumentFromArray
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getResourceIterator
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getIndexedClasses
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
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 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22declare(strict_types=1);
23
24namespace oat\tao\model\search\index;
25
26use oat\oatbox\service\ConfigurableService;
27use oat\tao\model\search\index\DocumentBuilder\IndexDocumentBuilder;
28use oat\tao\model\search\index\DocumentBuilder\IndexDocumentBuilderInterface;
29use oat\tao\model\search\Search;
30use oat\generis\model\OntologyAwareTrait;
31use oat\tao\model\menu\MenuService;
32use oat\tao\model\resources\ResourceIterator;
33
34/**
35 * Class IndexService
36 * @package oat\tao\model\search\index
37 */
38class IndexService extends ConfigurableService
39{
40    use OntologyAwareTrait;
41
42    public const SERVICE_ID = 'tao/IndexService';
43
44    /**
45     * Run a full reindexing
46     * @return int
47     * @throws
48     */
49    public function runIndexing(): int
50    {
51        $iterator = $this->getResourceIterator();
52        $indexIterator = new IndexIterator($iterator);
53        $indexIterator->setServiceLocator($this->getServiceLocator());
54        $searchService = $this->getServiceLocator()->get(Search::SERVICE_ID);
55        $result = $searchService->index($indexIterator);
56        $this->logDebug($result . ' resources have been indexed by ' . static::class);
57        return $result;
58    }
59
60    /**
61     * Returns IndexDocument Builder
62     *
63     * @return IndexDocumentBuilderInterface
64     * @deprecated use DI Container instead
65     */
66    public function getDocumentBuilder(): IndexDocumentBuilderInterface
67    {
68        return $this->getServiceLocator()->getContainer()->get(IndexDocumentBuilderInterface::class);
69    }
70
71    /**
72     * Create IndexDocument from core_kernel_classes_Resource
73     * @param \core_kernel_classes_Resource $resource
74     * @return IndexDocument
75     * @throws \common_Exception
76     * @throws \common_exception_InconsistentData
77     *
78     * @deprecated should be IndexDocumentBuilder::createDocumentFromResource instead
79     */
80    public function createDocumentFromResource(\core_kernel_classes_Resource $resource): IndexDocument
81    {
82        trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
83
84        /** @var IndexDocumentBuilder $documentBuilder */
85        $documentBuilder = $this->getDocumentBuilder();
86
87        return $documentBuilder->createDocumentFromResource($resource);
88    }
89
90    /**
91     * Create IndexDocument from array
92     * @param array $array
93     * @return IndexDocument
94     * @throws \common_exception_MissingParameter
95     * @throws \common_Exception
96     *
97     * @deprecated should be IndexDocumentBuilder::createDocumentFromArray instead
98     */
99    public function createDocumentFromArray($array = []): IndexDocument
100    {
101        trigger_error('Method ' . __METHOD__ . ' is deprecated', E_USER_DEPRECATED);
102
103        if (!isset($array['body'])) {
104            throw new \common_exception_MissingParameter('body');
105        }
106        if (!isset($array['id'])) {
107            throw new \common_exception_MissingParameter('id');
108        }
109
110        return $this->getDocumentBuilder()->createDocumentFromArray($array);
111    }
112
113    /**
114     * @return \Iterator
115     */
116    protected function getResourceIterator()
117    {
118        $iterator = new ResourceIterator($this->getIndexedClasses());
119        $iterator->setServiceLocator($this->getServiceLocator());
120        return $iterator;
121    }
122
123    protected function getIndexedClasses()
124    {
125        $classes = [];
126        foreach (MenuService::getAllPerspectives() as $perspective) {
127            foreach ($perspective->getChildren() as $structure) {
128                foreach ($structure->getTrees() as $tree) {
129                    $rootNode = $tree->get('rootNode');
130                    if (!empty($rootNode)) {
131                        $classes[$rootNode] = $this->getClass($rootNode);
132                    }
133                }
134            }
135        }
136        return array_values($classes);
137    }
138}