Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
QtiLanguageRetriever
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
5
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
 __invoke
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
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\Translation\Service;
24
25use core_kernel_classes_Resource;
26use oat\taoQtiItem\model\qti\Service;
27use Psr\Log\LoggerInterface;
28use Throwable;
29
30class QtiLanguageRetriever
31{
32    private Service $qtiItemService;
33    private LoggerInterface $logger;
34
35    public function __construct(Service $qtiItemService, LoggerInterface $logger)
36    {
37        $this->qtiItemService = $qtiItemService;
38        $this->logger = $logger;
39    }
40
41    public function __invoke(core_kernel_classes_Resource $item): ?string
42    {
43        try {
44            $itemData = $this->qtiItemService->getDataItemByRdfItem($item);
45        } catch (Throwable $exception) {
46            $this->logger->error(
47                sprintf(
48                    'An error occurred while retrieving item data: %s. Trace: %s',
49                    $exception->getMessage(),
50                    $exception->getTraceAsString()
51                )
52            );
53
54            throw $exception;
55        }
56
57        return $itemData && $itemData->hasAttribute('xml:lang')
58            ? $itemData->getAttributeValue('xml:lang')
59            : null;
60    }
61}