DiskSizeValidator.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators;
  3. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
  4. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Validators\BaseValidator;
  5. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits\WhmcsParams;
  6. use ModulesGarden\Servers\ProxmoxCloudVps\App\Enum\ConfigurableOption;
  7. class DiskSizeValidator extends BaseValidator
  8. {
  9. use WhmcsParams;
  10. protected $additionalField;
  11. protected $minValue = 0;
  12. protected $maxValue = 0;
  13. protected $required = true;
  14. public function __construct( $additionalField, $required = true)
  15. {
  16. $this->additionalField = $additionalField;
  17. $this->required = $required;
  18. }
  19. protected function validate($data, $additionalData = null)
  20. {
  21. if (!$this->required && empty($data))
  22. {
  23. return true;
  24. }
  25. $resurceManager = new ResourceManager();
  26. $additionalSize = 0;
  27. if($additionalData->get('formData')[$this->additionalField] ){
  28. $additionalSize = (int) $additionalData->get('formData')[$this->additionalField];
  29. }
  30. $diskResource = $resurceManager ->disk();
  31. $diskResource->setTotal($diskResource->getTotal()-$additionalSize);
  32. if ($this->isWhmcsConfigOption(ConfigurableOption::STORAGE)) {
  33. $this->maxValue = $this->getWhmcsConfigOption(ConfigurableOption::STORAGE) - $diskResource->getUsed();
  34. } else {
  35. $this->maxValue = $diskResource->free();
  36. }
  37. logModuleCall(
  38. 'ProxmoxCloudVps',
  39. __FUNCTION__,
  40. 'min: ' . $this->minValue . ' max: ' . $this->maxValue,
  41. 'Debug1',
  42. 'data: ' . $data . ' resMax: ' . $diskResource->getMax() . ' free: ' . $diskResource->free()
  43. );
  44. if ($this->maxValue > $diskResource->getMax()) {
  45. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $diskResource->getMax()]);
  46. return false;
  47. }
  48. $this->minValue = $diskResource->getMin();
  49. if(preg_match("/\./", $data)){
  50. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  51. return false;
  52. }
  53. if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
  54. {
  55. return true;
  56. }
  57. //Min & Max
  58. if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
  59. {
  60. return true;
  61. }
  62. //Min
  63. else
  64. {
  65. if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
  66. {
  67. return true;
  68. }
  69. }
  70. if ($this->minValue === $this->maxValue)
  71. {
  72. $this->addValidationError('PleaseProvideANumericValue');
  73. return false;
  74. }
  75. if (is_numeric($this->minValue) && is_numeric($this->maxValue))
  76. {
  77. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  78. }
  79. else
  80. {
  81. if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
  82. {
  83. $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
  84. }
  85. }
  86. return false;
  87. }
  88. }