Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| helpers_Report | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
132 | |
0.00% |
0 / 1 |
| renderToCommandLine | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
132 | |||
| 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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | use oat\generis\Helper\SystemHelper; |
| 23 | use oat\oatbox\reporting\ReportInterface; |
| 24 | |
| 25 | /** |
| 26 | * This helper aims at providing utility methods to render |
| 27 | * reports into TXT. |
| 28 | * |
| 29 | * |
| 30 | */ |
| 31 | class helpers_Report |
| 32 | { |
| 33 | public const AUTOSENSE = 'autosense'; |
| 34 | |
| 35 | /** |
| 36 | * Contains the logic to render a report and its children to the command line |
| 37 | * |
| 38 | * @param ReportInterface $report A report to be rendered. |
| 39 | * @param boolean $useColor |
| 40 | * @param integer $intend the intend of the message. |
| 41 | * @return string The shell output of $report. |
| 42 | */ |
| 43 | public static function renderToCommandLine( |
| 44 | ReportInterface $report, |
| 45 | $useColor = self::AUTOSENSE, |
| 46 | $intend = 0 |
| 47 | ): string { |
| 48 | switch ($report->getType()) { |
| 49 | case ReportInterface::TYPE_SUCCESS: |
| 50 | $color = '0;32'; // green |
| 51 | break; |
| 52 | |
| 53 | case ReportInterface::TYPE_WARNING: |
| 54 | $color = '1;33'; // yellow |
| 55 | break; |
| 56 | |
| 57 | case ReportInterface::TYPE_ERROR: |
| 58 | $color = '1;31'; // red |
| 59 | break; |
| 60 | |
| 61 | default: |
| 62 | $color = '0;37'; // light grey |
| 63 | } |
| 64 | if ($useColor === self::AUTOSENSE) { |
| 65 | $useColor = getenv('TAO_CONSOLE') !== 'nocolor' && !SystemHelper::isWindows(); |
| 66 | } |
| 67 | |
| 68 | $output = ($useColor ? "\033[" . $color . 'm' : '') |
| 69 | . ($intend > 0 ? str_repeat(' ', $intend) : '') |
| 70 | . $report->getMessage() |
| 71 | . ($useColor ? "\033[0m" : '') . PHP_EOL; |
| 72 | foreach ($report as $child) { |
| 73 | $output .= self::renderToCommandline($child, $useColor, $intend + 2); |
| 74 | } |
| 75 | |
| 76 | return $output; |
| 77 | } |
| 78 | } |