DiskSizeValidator.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. # find available space in VDC
  32. $diskResource->setTotal($diskResource->getTotal()-$additionalSize);
  33. if ($this->isWhmcsConfigOption(ConfigurableOption::STORAGE)) {
  34. $this->maxValue = $this->getWhmcsConfigOption(ConfigurableOption::STORAGE) - $diskResource->getUsed();
  35. } else {
  36. $this->maxValue = $diskResource->free();
  37. }
  38. # reduce available space to server limits
  39. if ($this->maxValue > $diskResource->getMax()) {
  40. $this->maxValue = $diskResource->getMax();
  41. }
  42. $this->minValue = $diskResource->getMin();
  43. # reject floating point
  44. if(preg_match("/\./", $data)){
  45. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  46. return false;
  47. }
  48. # accept if no limits
  49. if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
  50. {
  51. return true;
  52. }
  53. //Min & Max
  54. if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
  55. {
  56. return true;
  57. }
  58. //Min
  59. else
  60. {
  61. if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
  62. {
  63. return true;
  64. }
  65. }
  66. if ($this->minValue === $this->maxValue)
  67. {
  68. $this->addValidationError('PleaseProvideANumericValue');
  69. return false;
  70. }
  71. if (is_numeric($this->minValue) && is_numeric($this->maxValue))
  72. {
  73. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  74. }
  75. else
  76. {
  77. if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
  78. {
  79. $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
  80. }
  81. }
  82. return false;
  83. }
  84. }