Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
32.14% covered (danger)
32.14%
9 / 28
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractRdsSearcher
32.14% covered (danger)
32.14%
9 / 28
0.00% covered (danger)
0.00%
0 / 3
12.81
0.00% covered (danger)
0.00%
0 / 1
 getTargetClasses
n/a
0 / 0
n/a
0 / 0
0
 search
50.00% covered (danger)
50.00%
4 / 8
0.00% covered (danger)
0.00%
0 / 1
2.50
 getPersistenceIterator
35.71% covered (danger)
35.71%
5 / 14
0.00% covered (danger)
0.00%
0 / 1
3.06
 getFilterArray
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 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) 2020 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoMediaManager\model\relation\service;
24
25use common_persistence_Persistence;
26use common_persistence_sql_QueryIterator;
27use common_persistence_SqlPersistence;
28use core_kernel_persistence_Exception;
29use Doctrine\DBAL\Connection;
30use oat\generis\model\OntologyAwareTrait;
31use oat\generis\model\OntologyRdf;
32use oat\oatbox\service\ConfigurableService;
33use oat\tao\model\task\migration\ResultUnit;
34use oat\tao\model\task\migration\ResultUnitCollection;
35use oat\tao\model\task\migration\service\ResultFilter;
36use oat\tao\model\task\migration\service\ResultSearcherInterface;
37
38abstract class AbstractRdsSearcher extends ConfigurableService implements ResultSearcherInterface
39{
40    use OntologyAwareTrait;
41
42    abstract protected function getTargetClasses(): array;
43
44    public function search(ResultFilter $filter): ResultUnitCollection
45    {
46        $results = $this->getPersistenceIterator(
47            $filter->getParameter('start'),
48            $filter->getParameter('end')
49        );
50
51        $resultUnitCollection = new ResultUnitCollection();
52        foreach ($results as $result) {
53            $resultUnitCollection->add(new ResultUnit($this->getResource($result['subject'])));
54        }
55
56        return $resultUnitCollection;
57    }
58
59    private function getPersistenceIterator(int $start, int $end): iterable
60    {
61        /** @var common_persistence_Persistence $persistence */
62        $persistence = $this->getModel()->getPersistence();
63
64        if (!($persistence instanceof common_persistence_SqlPersistence)) {
65            throw new core_kernel_persistence_Exception(
66                'Persistence implementation has to be common_persistence_SqlPersistence instance'
67            );
68        }
69
70        $query = 'SELECT id, subject FROM statements WHERE (id BETWEEN :start AND :end) AND predicate = :predicate AND '
71            . 'object IN (:class) ORDER BY id';
72        $type['class'] = Connection::PARAM_STR_ARRAY;
73
74        return new common_persistence_sql_QueryIterator(
75            $persistence,
76            $query,
77            $this->getFilterArray($start, $end),
78            $type
79        );
80    }
81
82    private function getFilterArray(int $start, int $end): array
83    {
84        return [
85            'start' => $start,
86            'end' => $end,
87            'predicate' => OntologyRdf::RDF_TYPE,
88            'class' => array_unique($this->getTargetClasses()),
89        ];
90    }
91}