|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\TwoFactorAuth\Model\Config\Backend; |
| 9 | + |
| 10 | +use Magento\Framework\App\Config\Value; |
| 11 | +use Magento\Framework\App\Config\Data\ProcessorInterface; |
| 12 | +use Magento\Framework\Exception\ValidatorException; |
| 13 | +use OTPHP\TOTPInterface; |
| 14 | + |
| 15 | +class Leeway extends Value implements ProcessorInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Fetch Totp default period value |
| 19 | + * |
| 20 | + * @return int |
| 21 | + */ |
| 22 | + private function getDefaultPeriod(): int |
| 23 | + { |
| 24 | + return TOTPInterface::DEFAULT_PERIOD; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Process the value before saving. |
| 29 | + * |
| 30 | + * @param mixed $value The configuration value. |
| 31 | + * @return mixed The processed value. |
| 32 | + * @throws ValidatorException If the value is invalid. |
| 33 | + */ |
| 34 | + public function processValue($value) |
| 35 | + { |
| 36 | + if (!is_numeric($value)) { |
| 37 | + throw new ValidatorException(__('The Leeway must be a numeric value.')); |
| 38 | + } |
| 39 | + $numericValue = (int) $value; |
| 40 | + return $numericValue; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Validates the value before saving. |
| 45 | + * |
| 46 | + * @throws ValidatorException If the value is invalid. |
| 47 | + */ |
| 48 | + public function beforeSave() |
| 49 | + { |
| 50 | + $value = $this->getValue(); |
| 51 | + $period = $this->getDefaultPeriod(); |
| 52 | + if (!is_numeric($value) || $value < 1 || $value >= $period) { |
| 53 | + throw new ValidatorException( |
| 54 | + __( |
| 55 | + 'Invalid Leeway value. It must be between 1 and %1 as default period is %2', |
| 56 | + $period-1, |
| 57 | + $period |
| 58 | + ) |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + return parent::beforeSave(); |
| 63 | + } |
| 64 | +} |
0 commit comments