VM.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class VM
  5. */
  6. class VM
  7. {
  8. protected $config;
  9. public $virtualMachine;
  10. /**
  11. * VM constructor.
  12. * @param $config
  13. */
  14. public function __construct($config)
  15. {
  16. $this->config = $config;
  17. }
  18. /**
  19. * @return array
  20. */
  21. public function getConfig()
  22. {
  23. return $this->config;
  24. }
  25. /**
  26. * @return bool
  27. */
  28. public function isEnabled()
  29. {
  30. $config = $this->getConfig();
  31. if (!isset($config)) {
  32. return false;
  33. }
  34. if (!is_array($config)) {
  35. return false;
  36. }
  37. if (isset($config['enabled']) && $config['enabled']) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. /**
  43. * @return VirtualMachineInterface
  44. */
  45. public function getVirtualMachine()
  46. {
  47. return $this->virtualMachine;
  48. }
  49. /**
  50. * @param VirtualMachineInterface $virtualMachine
  51. */
  52. public function setVirtualMachine(VirtualMachineInterface $virtualMachine)
  53. {
  54. $this->virtualMachine = $virtualMachine;
  55. }
  56. /**
  57. * @return VirtualMachineInterface
  58. */
  59. public function getVirtualMachineFromConfig()
  60. {
  61. $vmList = $this->config['vms'];
  62. foreach ($vmList as $vm) {
  63. if (isset($vm['enabled']) && $vm['enabled'] == true) {
  64. $className = $vm['name'] . 'VM';
  65. return new $className($vm);
  66. break;
  67. }
  68. }
  69. return false;
  70. }
  71. /**
  72. * Resize the VM to the max size
  73. */
  74. public function resizeToMaxLimit()
  75. {
  76. $virtualMachine = $this->getVirtualMachineFromConfig();
  77. $this->setVirtualMachine($virtualMachine);
  78. $virtualMachine->resizeToMaxLimit();
  79. }
  80. /**
  81. * Resize the VM to the min size
  82. */
  83. public function resizeToMinLimit()
  84. {
  85. $virtualMachine = $this->getVirtualMachineFromConfig();
  86. $this->setVirtualMachine($virtualMachine);
  87. $virtualMachine->resizeToMinLimit();
  88. }
  89. public function runCron()
  90. {
  91. $virtualMachine = $this->getVirtualMachineFromConfig();
  92. $this->setVirtualMachine($virtualMachine);
  93. $virtualMachine->runCron();
  94. }
  95. }