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 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ListElementsFinder
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 find
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 setRequestOffset
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 setRequestLimit
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getLimit
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
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\taoBackOffice\model\ListElement\Service;
24
25use core_kernel_classes_Class;
26use oat\tao\model\Context\ContextInterface;
27use oat\tao\model\Lists\Business\Domain\ValueCollection;
28use oat\tao\model\Specification\ClassSpecificationInterface;
29use oat\tao\model\Lists\Business\Service\ValueCollectionService;
30use oat\tao\model\Lists\Business\Input\ValueCollectionSearchInput;
31use oat\tao\model\Lists\Business\Domain\ValueCollectionSearchRequest;
32use oat\taoBackOffice\model\ListElement\Context\ListElementsFinderContext;
33use oat\taoBackOffice\model\ListElement\Contract\ListElementsFinderInterface;
34
35class ListElementsFinder implements ListElementsFinderInterface
36{
37    /** @var ClassSpecificationInterface */
38    private $remoteListClassSpecification;
39
40    /** @var ValueCollectionService */
41    private $valueCollectionService;
42
43    /** @var int */
44    private $localListElementsLimit;
45
46    /** @var int */
47    private $remoteListElementsLimit;
48
49    public function __construct(
50        ClassSpecificationInterface $remoteListClassSpecification,
51        ValueCollectionService $valueCollectionService,
52        int $localListElementsLimit,
53        int $remoteListElementsLimit
54    ) {
55        $this->remoteListClassSpecification = $remoteListClassSpecification;
56        $this->valueCollectionService = $valueCollectionService;
57        $this->localListElementsLimit = $localListElementsLimit;
58        $this->remoteListElementsLimit = $remoteListElementsLimit;
59    }
60
61    public function find(ContextInterface $context): ValueCollection
62    {
63        /** @var core_kernel_classes_Class $listClass */
64        $listClass = $context->getParameter(ListElementsFinderContext::PARAMETER_LIST_CLASS);
65
66        $request = new ValueCollectionSearchRequest();
67        $request->setValueCollectionUri($listClass->getUri());
68
69        $totalCount = $this->valueCollectionService->count(new ValueCollectionSearchInput($request));
70
71        $this
72            ->setRequestOffset($request, $context)
73            ->setRequestLimit($request, $context);
74
75        $valueCollection = $this->valueCollectionService->findAll(new ValueCollectionSearchInput($request));
76        $valueCollection->setTotalCount($totalCount);
77
78        return $valueCollection;
79    }
80
81    private function setRequestOffset(ValueCollectionSearchRequest $request, ContextInterface $context): self
82    {
83        $offset = $context->getParameter(ListElementsFinderContext::PARAMETER_OFFSET, 0);
84
85        if ($offset) {
86            $request->setOffset($offset);
87        }
88
89        return $this;
90    }
91
92    private function setRequestLimit(ValueCollectionSearchRequest $request, ContextInterface $context): void
93    {
94        $limit = $this->getLimit($context);
95
96        if ($limit) {
97            $request->setLimit($limit);
98        }
99    }
100
101    private function getLimit(ContextInterface $context): int
102    {
103        if ($context->getParameter(ListElementsFinderContext::PARAMETER_LIMIT) === null) {
104            /** @var core_kernel_classes_Class $listClass */
105            $listClass = $context->getParameter(ListElementsFinderContext::PARAMETER_LIST_CLASS);
106            $isRemoteList = $this->remoteListClassSpecification->isSatisfiedBy($listClass);
107
108            return $isRemoteList
109                ? $this->remoteListElementsLimit
110                : $this->localListElementsLimit;
111        }
112
113        return $context->getParameter(ListElementsFinderContext::PARAMETER_LIMIT);
114    }
115}