Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| RegisterPortableElement | |
0.00% |
0 / 29 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
| __invoke | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
56 | |||
| createFailure | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getSourceDirectory | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 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 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\model\portableElement\action; |
| 23 | |
| 24 | use common_ext_action_InstallAction; |
| 25 | use oat\oatbox\service\ServiceManager; |
| 26 | use oat\taoQtiItem\model\portableElement\exception\PortableElementVersionIncompatibilityException; |
| 27 | use oat\taoQtiItem\model\portableElement\PortableElementService; |
| 28 | use common_report_Report as Report; |
| 29 | |
| 30 | /** |
| 31 | * Class RegisterPortableElement |
| 32 | * Abstract invokable action to register a single portable element |
| 33 | * The inherited class only need to implement getSourceDirectory() |
| 34 | * |
| 35 | * @package oat\taoQtiItem\model\portableElement\action |
| 36 | */ |
| 37 | abstract class RegisterPortableElement extends common_ext_action_InstallAction |
| 38 | { |
| 39 | public function __invoke($params) |
| 40 | { |
| 41 | $service = new PortableElementService(); |
| 42 | $service->setServiceLocator(ServiceManager::getServiceManager()); |
| 43 | |
| 44 | $sourceDirectory = $this->getSourceDirectory(); |
| 45 | |
| 46 | if (empty($sourceDirectory)) { |
| 47 | return $this->createFailure('the source directory is empty'); |
| 48 | } |
| 49 | |
| 50 | if (!is_readable($sourceDirectory)) { |
| 51 | return $this->createFailure('the source directory does not exists or is not readable ' . $sourceDirectory); |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | $model = $service->getValidPortableElementFromDirectorySource($sourceDirectory); |
| 56 | if (empty($model)) { |
| 57 | return Report::createFailure('no valid portable element found in directory "' . $sourceDirectory . '"'); |
| 58 | } |
| 59 | if (!empty($params)) { |
| 60 | $minRequiredVersion = $params[0]; |
| 61 | // if the minimal required version number string "x.y.z" is given in the parameter, |
| 62 | // the new target version should be equal or higher than it |
| 63 | if (version_compare($model->getVersion(), $minRequiredVersion) < 0) { |
| 64 | return $this->createFailure( |
| 65 | 'the version in manifest "' . $model->getVersion() |
| 66 | . '" cannot be lower than the given minimum required version "' . $minRequiredVersion . '"', |
| 67 | $model |
| 68 | ); |
| 69 | } |
| 70 | } |
| 71 | $service->registerFromDirectorySource($sourceDirectory); |
| 72 | } catch (PortableElementVersionIncompatibilityException $e) { |
| 73 | return $this->createFailure('incompatible version: ' . $e->getMessage(), $model); |
| 74 | } |
| 75 | |
| 76 | return Report::createSuccess( |
| 77 | 'registered portable element "' . $model->getTypeIdentifier() . '" in version "' |
| 78 | . $model->getVersion() . '""' |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Create a formatted failure report and log a warning |
| 84 | * |
| 85 | * @param $userMessage |
| 86 | * @param null $model |
| 87 | * @return Report |
| 88 | */ |
| 89 | private function createFailure($userMessage, $model = null) |
| 90 | { |
| 91 | $typeIdentifier = is_null($model) ? 'unknown type' : $model->getTypeIdentifier(); |
| 92 | $message = 'The portable element cannot be registered "' . $typeIdentifier . '", reason: ' . $userMessage; |
| 93 | \common_Logger::w($message); |
| 94 | return Report::createFailure($message); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Return the absolute path to the source directory |
| 99 | * |
| 100 | * @return string |
| 101 | */ |
| 102 | abstract protected function getSourceDirectory(); |
| 103 | } |