RessourceValidator.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. use ModulesGarden\Servers\ProxmoxCloudVps\App\Enum\ConfigurableOption;
  7. class RessourceValidator 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. $templateName = (int) $additionalData->get('formData')[$this->additionalField];
  28. $diskResource = $resurceManager ->disk();
  29. logModuleCall(
  30. 'proxmoxCloud',
  31. __FUNCTION__,
  32. $templateName,
  33. 'Debug',
  34. $diskResource
  35. );
  36. return true;
  37. $diskResource->setTotal($diskResource->getTotal()-$additionalSize);
  38. if ($this->isWhmcsConfigOption(ConfigurableOption::STORAGE)) {
  39. $this->maxValue = $this->getWhmcsConfigOption(ConfigurableOption::STORAGE) - $diskResource->getUsed();
  40. } else {
  41. $this->maxValue = $diskResource->free();
  42. }
  43. if ($this->maxValue > $diskResource->getMax()) {
  44. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $diskResource->getMax()]);
  45. return false;
  46. }
  47. $this->minValue = $diskResource->getMin();
  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. }