DiskSizeValidator.php 3.3 KB

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