Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
tao_actions_Languages
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
30
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
 index
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 getLanguages
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
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-2022 (original work) Open Assessment Technologies SA;
19 */
20
21declare(strict_types=1);
22
23use oat\generis\model\data\Ontology;
24use oat\tao\model\http\formatter\ResponseFormatter;
25use oat\tao\model\http\response\ErrorJsonResponse;
26use oat\tao\model\http\response\SuccessJsonResponse;
27use oat\tao\model\Language\Business\Contract\LanguageRepositoryInterface;
28use oat\tao\model\Language\Filter\LanguageAllowedFilter;
29use oat\tao\model\routing\Contract\ActionInterface;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32
33class tao_actions_Languages implements ActionInterface
34{
35    private ResponseFormatter $responseFormatter;
36    private LanguageRepositoryInterface $languageRepository;
37    private Ontology $ontology;
38    private LanguageAllowedFilter $languageAllowedFilter;
39
40    public function __construct(
41        ResponseFormatter $responseFormatter,
42        LanguageRepositoryInterface $languageRepository,
43        Ontology $ontology,
44        LanguageAllowedFilter $languageAllowedFilter
45    ) {
46        $this->responseFormatter = $responseFormatter;
47        $this->languageRepository = $languageRepository;
48        $this->ontology = $ontology;
49        $this->languageAllowedFilter = $languageAllowedFilter;
50    }
51
52    public function index(ResponseInterface $response, ServerRequestInterface $request): ResponseInterface
53    {
54        try {
55            $this->responseFormatter
56                ->withExpiration(time() + 60);
57
58            return $this->responseFormatter
59                ->withJsonHeader()
60                ->withStatusCode(200)
61                ->withBody(new SuccessJsonResponse($this->getLanguages($request)))
62                ->format($response);
63        } catch (Throwable $exception) {
64            return $this->responseFormatter
65                ->withJsonHeader()
66                ->withStatusCode(400)
67                ->withBody(new ErrorJsonResponse(0, $exception->getMessage(), []))
68                ->format($response);
69        }
70    }
71
72    private function getLanguages(ServerRequestInterface $request): array
73    {
74        $version = $request->getHeader('Accept-version')[0] ?? 'v1';
75
76        if ($version === 'v2') {
77            return $this->languageAllowedFilter
78                ->filterByLanguageCollection($this->languageRepository->findAvailableLanguagesByUsage())
79                ->jsonSerialize();
80        }
81
82        return tao_helpers_I18n::getAvailableLangsByUsage(
83            $this->ontology->getResource(tao_models_classes_LanguageService::INSTANCE_LANGUAGE_USAGE_DATA)
84        );
85    }
86}