Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ItemIdentifierValidator
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
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
 validate
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 overrideEnvironmentVariablePattern
100.00% covered (success)
100.00%
2 / 2
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) 2021 (original work) Open Assessment Technologies SA;
19 *
20 */
21
22declare(strict_types=1);
23
24namespace oat\taoQtiItem\model\qti\validator;
25
26use common_exception_Error;
27use oat\tao\model\featureFlag\FeatureFlagChecker;
28use oat\taoQtiItem\model\FeatureFlag\UniqueNumericQtiIdentifierClientConfig;
29use oat\taoQtiItem\model\qti\Item;
30
31class ItemIdentifierValidator
32{
33    public const ENV_QTI_IDENTIFIER_VALIDATOR_PATTERN = 'ENV_QTI_IDENTIFIER_VALIDATOR_PATTERN';
34
35    public const DEFAULT_PATTERN_PARAMETER_NAME = 'ItemIdentifierValidatorParameterName';
36    public const DEFAULT_PATTERN = '/^[a-zA-Z_]{1}[a-zA-Z0-9_\.-]*$/u';
37
38    /** @var string */
39    private $pattern;
40    private FeatureFlagChecker $featureFlagChecker;
41
42    public function __construct(FeatureFlagChecker $featureFlagChecker, string $pattern = self::DEFAULT_PATTERN)
43    {
44        $this->pattern = $pattern;
45        $this->featureFlagChecker = $featureFlagChecker;
46    }
47
48    /**
49     * @throws common_exception_Error
50     */
51    public function validate(Item $item): void
52    {
53        $this->overrideEnvironmentVariablePattern();
54        if (preg_match($this->pattern, $item->getAttributeValue('identifier')) !== 1) {
55            throw new common_exception_Error("The item identifier is not valid");
56        }
57    }
58
59    private function overrideEnvironmentVariablePattern(): void
60    {
61        if ($this->featureFlagChecker->isEnabled('FEATURE_FLAG_UNIQUE_NUMERIC_QTI_IDENTIFIER')) {
62            $this->pattern = UniqueNumericQtiIdentifierClientConfig::QTI_ID_PATTERN;
63        }
64    }
65}