Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| CliController | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 1 |
| runAction | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
30 | |||
| 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-2018 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\routing; |
| 23 | |
| 24 | use oat\oatbox\action\ActionService; |
| 25 | use oat\oatbox\action\ResolutionException; |
| 26 | use common_report_Report as Report; |
| 27 | use oat\tao\model\cliArgument\ArgumentService; |
| 28 | use oat\oatbox\service\ServiceManagerAwareTrait; |
| 29 | use oat\oatbox\service\ServiceManagerAwareInterface; |
| 30 | |
| 31 | /** |
| 32 | * Class CliController |
| 33 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 34 | * @package oat\tao\model\routing |
| 35 | */ |
| 36 | class CliController implements ServiceManagerAwareInterface |
| 37 | { |
| 38 | use ServiceManagerAwareTrait; |
| 39 | |
| 40 | /** |
| 41 | * @param string $actionIdentifier fully qualified action class name |
| 42 | * @param array $params Params to be passed to action's __invoke method |
| 43 | * @return Report |
| 44 | */ |
| 45 | public function runAction($actionIdentifier, array $params = []) |
| 46 | { |
| 47 | try { |
| 48 | $actionService = $this->getServiceLocator()->get(ActionService::SERVICE_ID); |
| 49 | $action = $actionService->resolve($actionIdentifier); |
| 50 | } catch (ResolutionException $e) { |
| 51 | return new Report(Report::TYPE_ERROR, $e->getMessage()); |
| 52 | } |
| 53 | |
| 54 | $this->propagate($action); |
| 55 | |
| 56 | $this->getServiceLocator()->get(ArgumentService::SERVICE_ID)->load($action, $params); |
| 57 | |
| 58 | try { |
| 59 | $report = call_user_func($action, $params); |
| 60 | if (empty($report)) { |
| 61 | $shortName = (new \ReflectionClass($action))->getName(); |
| 62 | $report = new \common_report_Report( |
| 63 | \common_report_Report::TYPE_INFO, |
| 64 | "Action '${shortName}' ended gracefully with no report returned." |
| 65 | ); |
| 66 | } |
| 67 | } catch (\Exception $e) { |
| 68 | $report = new Report(Report::TYPE_ERROR, __('An exception occured while running "%s"', $actionIdentifier)); |
| 69 | |
| 70 | $message = $e->getMessage(); |
| 71 | $previous = $e->getPrevious(); |
| 72 | |
| 73 | // Get the full stack trace of the exception |
| 74 | while ($previous) { |
| 75 | $message .= PHP_EOL . "caused by : " . PHP_EOL . $previous->getMessage(); |
| 76 | $previous = $previous->getPrevious(); |
| 77 | } |
| 78 | |
| 79 | $report->add(new Report(Report::TYPE_ERROR, $message)); |
| 80 | } |
| 81 | |
| 82 | return $report; |
| 83 | } |
| 84 | } |