| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace MGModule\DNSManager2\helpers\custom;
- use MGModule\DNSManager2\models\custom\zone\Repository;
- use MGModule\DNSManager2\models\custom\zone\ZoneTypeEnum;
- use MGModule\DNSManager2\models\whmcs\service\repository as ServiceRepository;
- use MGModule\DNSManager2\models\whmcs\domains\repository as DomainRepository;
- /**
- * Class ZoneLinked
- *
- * @author Artur Pilch <artur.pi@modulesgarden.com>
- */
- class ZoneLinked
- {
- private $connectedZone;
- private $zone;
- private $serverID;
- public function __construct($newZone = false, $serverID = false)
- {
- $this->zone = $newZone;
- $this->serverID = $serverID;
- if(!$this->serverID)
- {
- $this->serverID = $this->zone->serverid;
- }
- }
- private function checkAppendingZone()
- {
- if(!$this->zone || !$this->serverID)
- {
- return;
- }
- $repo = Repository::factory()->byName($this->zone->name)->byServerID($this->serverID)->one();
- if(empty($repo))
- {
- return;
- }
- $this->connectedZone->connectedWithType = 0;
- $this->connectedZone->connectedWithRelid = 0;
- if($repo->type == ZoneTypeEnum::DOMAIN && $this->zone->type == ZoneTypeEnum::HOSTING)
- {
- $this->connectedZone->connectedWithType = $this->zone->type;
- $this->connectedZone->connectedWithRelid = $this->zone->relid;
- }
- else if($repo->type == ZoneTypeEnum::HOSTING && $this->zone->type == ZoneTypeEnum::DOMAIN)
- {
- $this->connectedZone->connectedWithType = $this->zone->type;
- $this->connectedZone->connectedWithRelid = $this->zone->relid;
- }
- }
- private function checkIfZoneNeedConnect()
- {
- if(!$this->zone)
- {
- return;
- }
- $this->connectedZone->connectedWithType = 0;
- $this->connectedZone->connectedWithRelid = 0;
- if($this->zone->type == ZoneTypeEnum::HOSTING)
- {
- $rep = DomainRepository::factory()->byDomainName($this->zone->name)->one();
- if(!$rep)
- {
- return;
- }
- $this->connectedZone->connectedWithType = ZoneTypeEnum::DOMAIN;
- $this->connectedZone->connectedWithRelid = $rep->id;
- }
- else if($this->zone->type == ZoneTypeEnum::DOMAIN)
- {
- $rep = ServiceRepository::factory()->byDomainName($this->zone->name)->one();
- if(!$rep)
- {
- return;
- }
- $this->connectedZone->connectedWithType = ZoneTypeEnum::HOSTING;
- $this->connectedZone->connectedWithRelid = $rep->id;
- }
- }
- private function connectZones()
- {
- if(!$this->connectedZone || !$this->zone)
- {
- return false;
- }
- if($this->connectedZone->connectedWithType == 0 || $this->connectedZone->connectedWithRelid == 0)
- {
- return false;
- }
- $rep = Repository::factory()->byName($this->zone->name)->byServerID($this->serverID)->one();
- if(!$rep || $rep->connectedWithRelid == $this->connectedZone->connectedWithRelid)
- {
- return false;
- }
- $rep->connectedWithType = $this->connectedZone->connectedWithType;
- $rep->connectedWithRelid = $this->connectedZone->connectedWithRelid;
- $rep->save();
- return true;
- }
- public function connectByNewZone()
- {
- $this->checkAppendingZone();
- return $this->connectZones();
- }
- public function connectByService()
- {
- $this->checkIfZoneNeedConnect();
- $this->connectZones();
- }
- }
|