Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
43.24% |
16 / 37 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| PropertyWriter | |
43.24% |
16 / 37 |
|
40.00% |
2 / 5 |
43.90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| validate | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| write | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
| getPropertyToWrite | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| format | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2016 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\metadata\writer\ontologyWriter; |
| 23 | |
| 24 | use oat\generis\model\OntologyAwareTrait; |
| 25 | use oat\oatbox\service\ConfigurableService; |
| 26 | use oat\tao\helpers\form\ValidationRuleRegistry; |
| 27 | use oat\tao\model\metadata\exception\InconsistencyConfigException; |
| 28 | use oat\tao\model\metadata\exception\writer\MetadataWriterException; |
| 29 | |
| 30 | /** |
| 31 | * Class PropertyWriter |
| 32 | * Writer to write one value to a property |
| 33 | * |
| 34 | * @author Camille Moyon |
| 35 | * @package oat\tao\model\metadata\writer\ontologyWriter |
| 36 | */ |
| 37 | class PropertyWriter extends ConfigurableService implements OntologyWriter |
| 38 | { |
| 39 | use OntologyAwareTrait; |
| 40 | |
| 41 | public const PROPERTY_KEY = 'propertyUri'; |
| 42 | |
| 43 | /** |
| 44 | * PropertyWriter constructor. |
| 45 | * Check if property config key is set and if property exists |
| 46 | * |
| 47 | * @param array $params |
| 48 | * @throws InconsistencyConfigException |
| 49 | */ |
| 50 | public function __construct(array $params = []) |
| 51 | { |
| 52 | parent::__construct($params); |
| 53 | |
| 54 | if (! $this->hasOption(self::PROPERTY_KEY)) { |
| 55 | throw new InconsistencyConfigException( |
| 56 | 'Unable to find config key "' . self::PROPERTY_KEY . '" as property to write.' |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | if (! $this->getProperty($this->getOption(self::PROPERTY_KEY))->exists()) { |
| 61 | throw new InconsistencyConfigException('Config key "' . self::PROPERTY_KEY . '" found, ' . |
| 62 | 'but property "' . $this->getOption(self::PROPERTY_KEY) . '" does not exist.'); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Validate if value is writable by current writer |
| 68 | * Validation is handle by property validators |
| 69 | * |
| 70 | * @param $data |
| 71 | * @return bool |
| 72 | * @throws MetadataWriterException |
| 73 | */ |
| 74 | public function validate($data) |
| 75 | { |
| 76 | try { |
| 77 | /** @var \tao_helpers_form_Validator[] $validators */ |
| 78 | $validators = ValidationRuleRegistry::getRegistry()->getValidators($this->getPropertyToWrite()); |
| 79 | } catch (\common_exception_NotFound $e) { |
| 80 | throw new MetadataWriterException($e->getMessage()); |
| 81 | } |
| 82 | |
| 83 | $validated = true; |
| 84 | foreach ($validators as $validator) { |
| 85 | if (! $validator->evaluate($data)) { |
| 86 | $validated = false; |
| 87 | \common_Logger::d( |
| 88 | 'Unable to validate value for property "' . $this->getPropertyToWrite()->getUri() . '" ' |
| 89 | . 'against validator "' . $validator->getName() . '" : "' . $validator->getMessage() . '".' |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 | return $validated; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Write a value to a $resource |
| 98 | * |
| 99 | * @param \core_kernel_classes_Resource $resource |
| 100 | * @param $data |
| 101 | * @param bool $dryrun |
| 102 | * @return bool |
| 103 | * @throws MetadataWriterException |
| 104 | */ |
| 105 | public function write(\core_kernel_classes_Resource $resource, $data, $dryrun = false) |
| 106 | { |
| 107 | $propertyValue = $this->format($data); |
| 108 | |
| 109 | if ($this->validate($propertyValue)) { |
| 110 | if (! $dryrun) { |
| 111 | if (! $resource->editPropertyValues($this->getPropertyToWrite(), $propertyValue)) { |
| 112 | throw new MetadataWriterException( |
| 113 | 'A problem has occurred during writing property "' |
| 114 | . $this->getPropertyToWrite()->getUri() . '".' |
| 115 | ); |
| 116 | } |
| 117 | } |
| 118 | \common_Logger::d('Valid property "' . $this->getPropertyToWrite()->getUri() . '" ' . |
| 119 | 'to add to resource "' . $resource->getUri() . '" : ' . $propertyValue); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | throw new MetadataWriterException( |
| 124 | 'Writer "' . __CLASS__ . '" cannot validate value for property "' |
| 125 | . $this->getPropertyToWrite()->getUri() . '".' |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the property to be written |
| 131 | * |
| 132 | * @return \core_kernel_classes_Property |
| 133 | */ |
| 134 | protected function getPropertyToWrite() |
| 135 | { |
| 136 | return $this->getProperty($this->getOption(self::PROPERTY_KEY)); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Format data to be written |
| 141 | * |
| 142 | * @param array $data |
| 143 | * @return mixed |
| 144 | */ |
| 145 | public function format(array $data) |
| 146 | { |
| 147 | return array_pop($data); |
| 148 | } |
| 149 | } |