Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.44% |
17 / 18 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| AbstractContext | |
94.44% |
17 / 18 |
|
80.00% |
4 / 5 |
8.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| hasParameter | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getParameter | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
1.12 | |||
| setParameter | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getSupportedParameters | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| validateParameter | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| checkParameterSupport | |
100.00% |
7 / 7 |
|
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 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\generis\model\Context; |
| 24 | |
| 25 | use InvalidArgumentException; |
| 26 | |
| 27 | abstract class AbstractContext implements ContextInterface |
| 28 | { |
| 29 | protected $parameters = []; |
| 30 | |
| 31 | public function __construct(array $parameters) |
| 32 | { |
| 33 | foreach ($parameters as $parameter => $parameterValue) { |
| 34 | $this->setParameter($parameter, $parameterValue); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public function hasParameter(string $parameter): bool |
| 39 | { |
| 40 | try { |
| 41 | $this->checkParameterSupport($parameter); |
| 42 | } catch (InvalidArgumentException $exception) { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | return isset($this->parameters[$parameter]); |
| 47 | } |
| 48 | |
| 49 | public function getParameter(string $parameter, $default = null) |
| 50 | { |
| 51 | $this->checkParameterSupport($parameter); |
| 52 | |
| 53 | return $this->parameters[$parameter] ?? $default; |
| 54 | } |
| 55 | |
| 56 | public function setParameter(string $parameter, $parameterValue): void |
| 57 | { |
| 58 | $this->checkParameterSupport($parameter); |
| 59 | $this->validateParameter($parameter, $parameterValue); |
| 60 | |
| 61 | $this->parameters[$parameter] = $parameterValue; |
| 62 | } |
| 63 | |
| 64 | abstract protected function getSupportedParameters(): array; |
| 65 | |
| 66 | /** |
| 67 | * @param mixed $parameterValue |
| 68 | */ |
| 69 | abstract protected function validateParameter(string $parameter, $parameterValue): void; |
| 70 | |
| 71 | private function checkParameterSupport(string $parameter): void |
| 72 | { |
| 73 | if (!in_array($parameter, $this->getSupportedParameters(), true)) { |
| 74 | throw new InvalidArgumentException( |
| 75 | sprintf( |
| 76 | 'Context parameter %s is not supported.', |
| 77 | $parameter |
| 78 | ) |
| 79 | ); |
| 80 | } |
| 81 | } |
| 82 | } |