Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
MetadataAliasesExtractor
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
4 / 4
6
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
 extract
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getHash
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 extractAliases
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
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) 2022 (original work) Open Assessment Technologies SA.
19 */
20
21declare(strict_types=1);
22
23namespace oat\tao\model\StatisticalMetadata\Import\Extractor;
24
25use oat\tao\model\StatisticalMetadata\Contract\Header;
26
27class MetadataAliasesExtractor
28{
29    /** @var MetadataHeadersExtractor */
30    private $metadataHeadersExtractor;
31
32    /** @var array */
33    private $cache = [];
34
35    public function __construct(MetadataHeadersExtractor $metadataHeadersExtractor)
36    {
37        $this->metadataHeadersExtractor = $metadataHeadersExtractor;
38    }
39
40    public function extract(array $header): array
41    {
42        $hash = $this->getHash($header);
43
44        if (!isset($this->cache[$hash])) {
45            $this->cache[$hash] = $this->extractAliases($this->metadataHeadersExtractor->extract($header));
46        }
47
48        return $this->cache[$hash];
49    }
50
51    private function getHash(array $header): string
52    {
53        sort($header);
54
55        return hash('sha256', json_encode($header));
56    }
57
58    private function extractAliases(array $metadata): array
59    {
60        $aliases = [];
61
62        foreach ($metadata as $metadatum) {
63            $aliases[] = str_replace(Header::METADATA_PREFIX, '', $metadatum);
64        }
65
66        return $aliases;
67    }
68}