Strategy.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns;
  3. use MGModule\DNSManager2\mgLibs\custom\SubmoduleCap;
  4. use MGModule\DNSManager2\mgLibs\custom\TaskManager;
  5. use MGModule\DNSManager2\models\custom\zone\Repository;
  6. class Strategy
  7. {
  8. const PRE = 'pre';
  9. const AFTER = 'after';
  10. protected static $events = [
  11. 'addRecord' => [
  12. self::AFTER => 'RecordSynchronization:syncAdd'
  13. ],
  14. 'deleteRecord' => [
  15. self::AFTER => 'RecordSynchronization:syncDelete'
  16. ],
  17. 'editRecord' => [
  18. self::AFTER => 'RecordSynchronization:syncEdit'
  19. ],
  20. 'activateZone' => [
  21. self::AFTER => 'RecordSynchronization:syncActivateZone'
  22. ],
  23. 'terminateZone' => [
  24. self::AFTER => 'RecordSynchronization:syncTerminateZone'
  25. ],
  26. 'removeRDNS' => [
  27. self::AFTER => 'RecordSynchronization:syncRemoveRDNS'
  28. ],
  29. 'updateRDNS' => [
  30. self::AFTER => 'RecordSynchronization:syncUpdateRDNS'
  31. ],
  32. ];
  33. protected $module;
  34. public function __construct($module)
  35. {
  36. $this->module = new SubmoduleCap($module);
  37. }
  38. public function __call($name, $args)
  39. {
  40. $result = call_user_func_array(array($this->module,$name), $args);
  41. //$result = count($args) == 1 ? $this->module->{$name}($args[0]) : $this->module->{$name}($args);
  42. if (isset(self::$events[$name][self::AFTER]))
  43. {
  44. $this->addTask($name, $args, self::AFTER);
  45. }
  46. return $result;
  47. }
  48. public function __get($name)
  49. {
  50. return $this->module->{$name};
  51. }
  52. public function getModule()
  53. {
  54. return $this->module;
  55. }
  56. private function addTask($name, $args, $when)
  57. {
  58. $packageServerRepo = new \MGModule\DNSManager2\models\custom\package\server\Repository();
  59. $packageServer = $packageServerRepo->byServerID($this->module->getServer()->id)->get()[0];
  60. if (!$packageServer || !$packageServer->isMaster())
  61. {
  62. return;
  63. }
  64. switch ($name)
  65. {
  66. case "deleteRecord":
  67. case "editRecord":
  68. case "addRecord":
  69. if (($this->taskExistsWithStatusStart('deleteRecord', self::AFTER)
  70. || $this->taskExistsWithStatusStart('editRecord', self::AFTER)
  71. || $this->taskExistsWithStatusStart('addRecord', self::AFTER)))
  72. {
  73. return;
  74. }
  75. TaskManager::addTask(self::$events[$name][$when] . "_" . $this->module->getDomain(), ['submodule' => $this, 'values' => $args, 'zone' => $this->getZone()]);
  76. break;
  77. default:
  78. if (!$this->taskExistsWithStatusStart($name, $when))
  79. {
  80. TaskManager::addTask(self::$events[$name][$when] . "_" . $this->module->getDomain(), ['submodule' => $this, 'values' => $args, 'zone' => $this->getZone()]);
  81. }
  82. break;
  83. }
  84. }
  85. private function getZone()
  86. {
  87. $zoneRepository = Repository::factory();
  88. return $zoneRepository->byServerID($this->getServer()->id)->byName($this->getDomain())->get()[0];
  89. }
  90. private function taskExistsWithStatusStart(string $name, string $when): bool
  91. {
  92. return count(TaskManager::getTasksByNameAndStatus(self::$events[$name][$when] . "_" . $this->module->getDomain(), 'start')) > 0;
  93. }
  94. }