|
10 | 10 | namespace OxidEsales\SecurityModule\PasswordPolicy\Intrastructure; |
11 | 11 |
|
12 | 12 | use OxidEsales\Eshop\Core\Exception\InputException; |
| 13 | +use OxidEsales\Eshop\Core\Language; |
| 14 | +use OxidEsales\SecurityModule\PasswordPolicy\Validation\Exception\PasswordCollectionException; |
13 | 15 | use OxidEsales\SecurityModule\PasswordPolicy\Validation\Exception\PasswordValidateException; |
14 | 16 |
|
15 | | -class ExceptionFactory implements ExceptionFactoryInterface |
| 17 | +readonly class ExceptionFactory implements ExceptionFactoryInterface |
16 | 18 | { |
17 | 19 | public function __construct( |
18 | | - private readonly \OxidEsales\Eshop\Core\Language $language |
| 20 | + private Language $language |
19 | 21 | ) { |
20 | 22 | } |
21 | 23 |
|
22 | 24 | public function create(PasswordValidateException $exception): InputException |
23 | 25 | { |
24 | | - $exception = oxNew( |
25 | | - InputException::class, |
26 | | - sprintf( |
27 | | - /** @phpstan-ignore-next-line */ |
28 | | - $this->language->translateString($exception->getMessage()), |
29 | | - ...$exception->getTranslationParameters() |
30 | | - ) |
31 | | - ); |
| 26 | + if ($exception instanceof PasswordCollectionException) { |
| 27 | + return $this->createCollection($exception); |
| 28 | + } |
| 29 | + |
| 30 | + return oxNew(InputException::class, $this->formatExceptionMessage($exception)); |
| 31 | + } |
| 32 | + |
| 33 | + private function createCollection(PasswordCollectionException $collection): InputException |
| 34 | + { |
| 35 | + $lines = []; |
| 36 | + foreach ($collection->getValidationExceptions() as $exception) { |
| 37 | + $lines[] = $this->formatExceptionMessage($exception); |
| 38 | + } |
| 39 | + $message = '<ul><li>' . implode('</li><li>', $lines) . '</li></ul>'; |
32 | 40 |
|
33 | | - return $exception; |
| 41 | + return oxNew(InputException::class, $message); |
| 42 | + } |
| 43 | + |
| 44 | + private function formatExceptionMessage(PasswordValidateException $exception): string |
| 45 | + { |
| 46 | + return sprintf( |
| 47 | + /** @phpstan-ignore-next-line */ |
| 48 | + $this->language->translateString($exception->getMessage()), |
| 49 | + ...$exception->getTranslationParameters() |
| 50 | + ); |
34 | 51 | } |
35 | 52 | } |
0 commit comments