Resource.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services\Cloud;
  3. use ModulesGarden\ProxmoxAddon\App\Libs\Format;
  4. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  5. class Resource
  6. {
  7. protected $name;
  8. protected $unit;
  9. protected $min;
  10. protected $used;
  11. protected $max;
  12. protected $total;
  13. protected $free;
  14. /**
  15. * Resource constructor.
  16. * @param $name
  17. */
  18. public function __construct($name)
  19. {
  20. $this->name = $name;
  21. }
  22. /**
  23. * @return mixed
  24. */
  25. public function getUnit()
  26. {
  27. return $this->unit;
  28. }
  29. /**
  30. * @param mixed $unit
  31. * @return Resource
  32. */
  33. public function setUnit($unit)
  34. {
  35. $this->unit = $unit;
  36. return $this;
  37. }
  38. /**
  39. * @return mixed
  40. */
  41. public function getMin()
  42. {
  43. return $this->min;
  44. }
  45. /**
  46. * @param mixed $min
  47. * @return Resource
  48. */
  49. public function setMin($min)
  50. {
  51. $this->min = $min;
  52. return $this;
  53. }
  54. /**
  55. * @return mixed
  56. */
  57. public function getMax()
  58. {
  59. return $this->max;
  60. }
  61. /**
  62. * @param mixed $max
  63. * @return Resource
  64. */
  65. public function setMax($max)
  66. {
  67. $this->max = $max;
  68. return $this;
  69. }
  70. /**
  71. * @return mixed
  72. */
  73. public function getUsed()
  74. {
  75. return $this->used;
  76. }
  77. /**
  78. * @param mixed $used
  79. * @return Resource
  80. */
  81. public function setUsed($used)
  82. {
  83. $this->used = $used;
  84. return $this;
  85. }
  86. /**
  87. * @return mixed
  88. */
  89. public function getTotal()
  90. {
  91. return $this->total;
  92. }
  93. /**
  94. * @param mixed $total
  95. * @return Resource
  96. */
  97. public function setTotal($total)
  98. {
  99. $this->total = $total;
  100. return $this;
  101. }
  102. public function free(){
  103. $totalUsed = $this->freeTotal();
  104. if($this->max && $this->max < $totalUsed){
  105. return $this->max;
  106. }
  107. return $totalUsed;
  108. }
  109. public function freeTotal(){
  110. return $this->total - $this->used;
  111. }
  112. public function hasFreeTotal(){
  113. return $this->freeTotal() > 0;
  114. }
  115. public function getPercent(){
  116. if(!$this->total){
  117. return 0;
  118. }
  119. return round($this->used / $this->total * 100);
  120. }
  121. /**
  122. * @return mixed
  123. */
  124. public function getName()
  125. {
  126. return $this->name;
  127. }
  128. public function isUnlimited(){
  129. return $this->total == -1;
  130. }
  131. }