Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ListCreator
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
6
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
 createEmptyList
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 getListCount
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
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\lists;
24
25use Iterator;
26use oat\tao\model\Specification\ClassSpecificationInterface;
27
28class ListCreator
29{
30    /** @var ListService */
31    private $listService;
32
33    /** @var ClassSpecificationInterface */
34    private $remoteListClassSpecification;
35
36    public function __construct(
37        ListService $listService,
38        ClassSpecificationInterface $remoteListClassSpecification
39    ) {
40        $this->listService = $listService;
41        $this->remoteListClassSpecification = $remoteListClassSpecification;
42    }
43
44    public function createEmptyList(): ListCreatedResponse
45    {
46        $newName = __('List') . ' ' . ($this->getListCount() + 1);
47
48        $list = $this->listService->createList($newName);
49        $this->listService->createListElement($list, __('Element') . ' 1');
50
51        $elements = $this->listService->getListElements($list);
52
53        if ($elements instanceof Iterator) {
54            $elements = iterator_to_array($elements);
55        }
56
57        return new ListCreatedResponse($list, $elements);
58    }
59
60    private function getListCount(): int
61    {
62        $accumulator = 0;
63
64        // @todo Count from database instead of loading all lists in memory
65        foreach ($this->listService->getLists() as $listClass) {
66            if (!$this->remoteListClassSpecification->isSatisfiedBy($listClass)) {
67                ++$accumulator;
68            }
69        }
70
71        return $accumulator;
72    }
73}