RessourceValidator.php 2.9 KB

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