RessourceValidator.php 2.8 KB

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