Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.24% |
20 / 21 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Report | |
95.24% |
20 / 21 |
|
80.00% |
4 / 5 |
11 | |
0.00% |
0 / 1 |
| create | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setInterpolationMessage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| jsonUnserialize | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| translateMessage | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| 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) 2020-2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\oatbox\reporting; |
| 24 | |
| 25 | use common_exception_Error; |
| 26 | use common_report_Report; |
| 27 | |
| 28 | /** |
| 29 | * The Report allows to return a more detailed return value |
| 30 | * then a simple boolean variable denoting the success. |
| 31 | * |
| 32 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 33 | * @author Ivan Klimchuk, <ivan.klimchuk@1pt.com> |
| 34 | * |
| 35 | * @method static self createInfo(string $message, $data = null, array $children = []): self |
| 36 | * @method static self createSuccess(string $message, $data = null, array $children = []): self |
| 37 | * @method static self createWarning(string $message, $data = null, array $children = []): self |
| 38 | * @method static self createError(string $message, $data = null, array $children = []): self |
| 39 | * |
| 40 | * @method $this getInfos(bool $asFlat = false): array |
| 41 | * @method $this getSuccesses(bool $asFlat = false): array |
| 42 | * @method $this getWarnings(bool $asFlat = false): array |
| 43 | * @method $this getErrors(bool $asFlat = false): array |
| 44 | * |
| 45 | * @method $this containsInfo() Whenever or not the report contains info messages |
| 46 | * @method $this containsSuccess() Whenever or not the report contains successes |
| 47 | * @method $this containsWarning() Whenever or not the report contains warnings |
| 48 | * @method $this containsError() Whenever or not the report contains errors |
| 49 | */ |
| 50 | class Report extends common_report_Report |
| 51 | { |
| 52 | /** @var string */ |
| 53 | private $interpolationMessage; |
| 54 | |
| 55 | /** @var array */ |
| 56 | private $interpolationData; |
| 57 | |
| 58 | /** |
| 59 | * Create Report with translations support |
| 60 | * |
| 61 | * @throws common_exception_Error |
| 62 | */ |
| 63 | public static function create(string $type, string $interpolationMessage, array $interpolationData = []): Report |
| 64 | { |
| 65 | return (new self($type, sprintf($interpolationMessage, ...$interpolationData))) |
| 66 | ->setInterpolationMessage($interpolationMessage, $interpolationData); |
| 67 | } |
| 68 | |
| 69 | public function setInterpolationMessage(string $interpolationMessage, array $interpolationData = []): self |
| 70 | { |
| 71 | $this->interpolationMessage = $interpolationMessage; |
| 72 | $this->interpolationData = $interpolationData; |
| 73 | |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | public function jsonSerialize(): array |
| 78 | { |
| 79 | $data = parent::jsonSerialize(); |
| 80 | |
| 81 | if ($this->interpolationMessage) { |
| 82 | $data['interpolationMessage'] = $this->interpolationMessage; |
| 83 | } |
| 84 | |
| 85 | if ($this->interpolationData || $this->interpolationMessage) { |
| 86 | $data['interpolationData'] = $this->interpolationData; |
| 87 | } |
| 88 | |
| 89 | return $data; |
| 90 | } |
| 91 | |
| 92 | public static function jsonUnserialize($data): ?common_report_Report |
| 93 | { |
| 94 | /** @var Report $report */ |
| 95 | $report = parent::jsonUnserialize($data); |
| 96 | |
| 97 | if (isset($data['interpolationMessage'])) { |
| 98 | $report->setInterpolationMessage( |
| 99 | (string) $data['interpolationMessage'], |
| 100 | (array) ($data['interpolationData'] ?? []) |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | return $report; |
| 105 | } |
| 106 | |
| 107 | public function translateMessage(): string |
| 108 | { |
| 109 | if ($this->interpolationMessage && count($this->interpolationData) > 0) { |
| 110 | return __($this->interpolationMessage, ...$this->interpolationData); |
| 111 | } |
| 112 | |
| 113 | return __($this->interpolationMessage ?? $this->getMessage()); |
| 114 | } |
| 115 | } |