Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
16 / 18 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| AbstractOverriddenToolsRepository | |
88.89% |
16 / 18 |
|
66.67% |
2 / 3 |
5.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| findAll | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| findAllUnfiltered | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| fetchAvailableToolIds | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
3.07 | |||
| 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 | * @author Sergei Mikhailov <sergei.mikhailov@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\taoQtiTest\models\runner\toolsStates\DataAccess\Repository; |
| 26 | |
| 27 | use oat\tao\model\service\InjectionAwareService; |
| 28 | use oat\taoQtiTest\models\runner\config\Business\Contract\OverriddenOptionsRepositoryInterface; |
| 29 | use oat\taoQtiTest\models\runner\config\Business\Domain\Option; |
| 30 | use oat\taoQtiTest\models\runner\config\Business\Domain\OptionCollection; |
| 31 | use oat\taoQtiTest\models\TestCategoryPreset; |
| 32 | use oat\taoQtiTest\models\TestCategoryPresetProvider; |
| 33 | use RuntimeException; |
| 34 | |
| 35 | abstract class AbstractOverriddenToolsRepository extends InjectionAwareService implements |
| 36 | OverriddenOptionsRepositoryInterface |
| 37 | { |
| 38 | /** @var TestCategoryPresetProvider */ |
| 39 | protected $presetRepository; |
| 40 | |
| 41 | /** @var int[]|null */ |
| 42 | private $availableToolIds; |
| 43 | |
| 44 | /** @noinspection MagicMethodsValidityInspection */ |
| 45 | /** @noinspection PhpMissingParentConstructorInspection */ |
| 46 | public function __construct(TestCategoryPresetProvider $presetRepository) |
| 47 | { |
| 48 | $this->presetRepository = $presetRepository; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @inheritDoc |
| 53 | */ |
| 54 | public function findAll(): OptionCollection |
| 55 | { |
| 56 | return $this |
| 57 | ->findAllUnfiltered() |
| 58 | ->filter( |
| 59 | function (Option $option): bool { |
| 60 | return in_array($option->getId(), $this->fetchAvailableToolIds(), true); |
| 61 | } |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | abstract protected function findAllUnfiltered(): OptionCollection; |
| 66 | |
| 67 | private function fetchAvailableToolIds(): array |
| 68 | { |
| 69 | if (null === $this->availableToolIds) { |
| 70 | try { |
| 71 | $this->availableToolIds = array_map( |
| 72 | static function (TestCategoryPreset $preset): string { |
| 73 | return $preset->getId(); |
| 74 | }, |
| 75 | $this->presetRepository->findPresetGroupOrFail(TestCategoryPresetProvider::GROUP_TOOLS)['presets'] |
| 76 | ); |
| 77 | } catch (RuntimeException $exception) { |
| 78 | $this->availableToolIds = []; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $this->availableToolIds; |
| 83 | } |
| 84 | } |