ZoneLinked.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace MGModule\DNSManager2\helpers\custom;
  3. use MGModule\DNSManager2\models\custom\zone\Repository;
  4. use MGModule\DNSManager2\models\custom\zone\ZoneTypeEnum;
  5. use MGModule\DNSManager2\models\whmcs\service\repository as ServiceRepository;
  6. use MGModule\DNSManager2\models\whmcs\domains\repository as DomainRepository;
  7. /**
  8. * Class ZoneLinked
  9. *
  10. * @author Artur Pilch <artur.pi@modulesgarden.com>
  11. */
  12. class ZoneLinked
  13. {
  14. private $connectedZone;
  15. private $zone;
  16. private $serverID;
  17. public function __construct($newZone = false, $serverID = false)
  18. {
  19. $this->zone = $newZone;
  20. $this->serverID = $serverID;
  21. if(!$this->serverID)
  22. {
  23. $this->serverID = $this->zone->serverid;
  24. }
  25. }
  26. private function checkAppendingZone()
  27. {
  28. if(!$this->zone || !$this->serverID)
  29. {
  30. return;
  31. }
  32. $repo = Repository::factory()->byName($this->zone->name)->byServerID($this->serverID)->one();
  33. if(empty($repo))
  34. {
  35. return;
  36. }
  37. $this->connectedZone->connectedWithType = 0;
  38. $this->connectedZone->connectedWithRelid = 0;
  39. if($repo->type == ZoneTypeEnum::DOMAIN && $this->zone->type == ZoneTypeEnum::HOSTING)
  40. {
  41. $this->connectedZone->connectedWithType = $this->zone->type;
  42. $this->connectedZone->connectedWithRelid = $this->zone->relid;
  43. }
  44. else if($repo->type == ZoneTypeEnum::HOSTING && $this->zone->type == ZoneTypeEnum::DOMAIN)
  45. {
  46. $this->connectedZone->connectedWithType = $this->zone->type;
  47. $this->connectedZone->connectedWithRelid = $this->zone->relid;
  48. }
  49. }
  50. private function checkIfZoneNeedConnect()
  51. {
  52. if(!$this->zone)
  53. {
  54. return;
  55. }
  56. $this->connectedZone->connectedWithType = 0;
  57. $this->connectedZone->connectedWithRelid = 0;
  58. if($this->zone->type == ZoneTypeEnum::HOSTING)
  59. {
  60. $rep = DomainRepository::factory()->byDomainName($this->zone->name)->one();
  61. if(!$rep)
  62. {
  63. return;
  64. }
  65. $this->connectedZone->connectedWithType = ZoneTypeEnum::DOMAIN;
  66. $this->connectedZone->connectedWithRelid = $rep->id;
  67. }
  68. else if($this->zone->type == ZoneTypeEnum::DOMAIN)
  69. {
  70. $rep = ServiceRepository::factory()->byDomainName($this->zone->name)->one();
  71. if(!$rep)
  72. {
  73. return;
  74. }
  75. $this->connectedZone->connectedWithType = ZoneTypeEnum::HOSTING;
  76. $this->connectedZone->connectedWithRelid = $rep->id;
  77. }
  78. }
  79. private function connectZones()
  80. {
  81. if(!$this->connectedZone || !$this->zone)
  82. {
  83. return false;
  84. }
  85. if($this->connectedZone->connectedWithType == 0 || $this->connectedZone->connectedWithRelid == 0)
  86. {
  87. return false;
  88. }
  89. $rep = Repository::factory()->byName($this->zone->name)->byServerID($this->serverID)->one();
  90. if(!$rep || $rep->connectedWithRelid == $this->connectedZone->connectedWithRelid)
  91. {
  92. return false;
  93. }
  94. $rep->connectedWithType = $this->connectedZone->connectedWithType;
  95. $rep->connectedWithRelid = $this->connectedZone->connectedWithRelid;
  96. $rep->save();
  97. return true;
  98. }
  99. public function connectByNewZone()
  100. {
  101. $this->checkAppendingZone();
  102. return $this->connectZones();
  103. }
  104. public function connectByService()
  105. {
  106. $this->checkIfZoneNeedConnect();
  107. $this->connectZones();
  108. }
  109. }