Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ChecksumGenerator
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRangeChecksum
100.00% covered (success)
100.00%
13 / 13
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) 2024 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoQtiItem\model\import;
24
25use core_kernel_classes_Property as Property;
26use InvalidArgumentException;
27use oat\taoBackOffice\model\lists\ListService;
28
29class ChecksumGenerator
30{
31    private ListService $listService;
32
33    public function __construct(ListService $listService)
34    {
35        $this->listService = $listService;
36    }
37
38    public function getRangeChecksum(Property $property): string
39    {
40        if ($property->getRange() === null) {
41            throw new InvalidArgumentException(
42                sprintf(
43                    'Property %s does not have range set. Only properties with range can have checksum',
44                    $property->getUri()
45                )
46            );
47        }
48
49        $labels = [];
50        foreach ($this->listService->getListElements($property->getRange()) as $listEntry) {
51            $labels[] = strtolower($listEntry->getLabel());
52        }
53        asort($labels);
54        $checksum = implode('', $labels);
55
56        return sha1(trim($checksum));
57    }
58}