<?php


/**
 * Description of RDNS
 *
 * @author vitalii@modulesgarden.com
 */

namespace MGModule\DNSManager2\mgLibs\ModuleAPI;

use \MGModule\DNSManager2\mgLibs\custom\reverse\ReverseDNS;
use \MGModule\DNSManager2\models\custom\reverse\Repository;


class RDNS extends Api
{
    
    public $data;
    
    
    
    public function save( )
    {
        try {
            
            $reverse = Repository::factory()
                ->byServerID($this->data['serverid'])
                ->byIP($this->data['ip'])
                ->one();

            if($reverse == false) {
                $helper = new ReverseDNS();
                $helper->setClientID($this->data['clientid']?:'0');
                $helper->setIP($this->data['ip']);
                $helper->setServer($this->data['serverid']);
                $helper->setTTL($this->data['ttl']);
                $helper->setDomain($this->data['rdata']);
                $helper->create();
            } else {
                $helper = new ReverseDNS($reverse);
                $helper->update();
            }
        } catch (\Exception $ex) {
            
        }
    }
    
    
    
    public function remove(  )
    {
        $reverse = Repository::factory()
            ->byServerID($this->data['serverid'])
            ->byIP($this->data['ip'])
            ->one();
        if($reverse != false) {
            $helper = new ReverseDNS($reverse);
            $helper->remove();
        } else {
            $this->errors[] = 'Reverse not found in database';
        }
    }
    
    
}
