Just learn if and else... :s Optimization perhaps?

Hello,

Before you give up, you can take this as a test or challenge? :D:blush:

I’m wondering if anyone could help me to pass this, into a more elegant way of doing it.


$comandoController = new ComandoController();

// login
$comandoController->login($user, $pass);

//instantiate the VO
$dominioVo = new DominioVo();

//defines global variables to be used between several conditionals.
$hostNameInfo = '';
$hostAddressInfo = '';
$roidInfo = '';

//if our form for getting the info is submitted:
if (isset($_GET['infoDominio']))
{
     //grab the domain name value and filter it
     $nomeDominio = filter_input(INPUT_GET, 'nomeDominio', FILTER_SANITIZE_STRING);
     //set it
     $dominioVo->setNome($nomeDominio);
                
     try //try to run infoDomain command and retrieve the response from it
     {
         $infoDominio = $comandoController->infoDomain($dominioVo);
         //receives some info from #infoDominio returned response:
         $hostNameInfo = isset($infoDominio['hostName1']) ? $infoDominio['hostName1'] : '';

         $hostAddressInfo = isset($hostNameInfo) ? gethostbyname($hostNameInfo) : '';
         $roidInfo = isset($infoDominio['roid']) ? $infoDominio['roid'] : '';
       }
       catch (EppCommandsExceptions $e)
       {
           echo 'Exceção: ',  $e->getMessage();
       }         
}
            
//once the we collect the info, we can now POST that information and store it on a database or whatever. If a post has been made:
if (isset($_POST['atualizarDominio']))
{
  //receives and filters:
  $hostNameInput = filter_input(INPUT_POST, 'nsPrimario', FILTER_SANITIZE_STRING);
  $hostAddressInput = filter_input(INPUT_POST, 'nsEndereco', FILTER_SANITIZE_STRING);
   //if the command didn't return any ns AND the input fields are NOT empty.
   if (  ( (empty($hostNameInfo)) && (empty($hostAddressInfo)) ) && ((!empty($hostNameInput)) && (!empty($hostAddressInput)) )  )
   {
      $dominioVo->setNsNome($hostNameInput);
      $dominioVo->setNsEndereco($hostAddressInput);
      $dominioVo->setNumeroProcesso($roidInfo);

      try
      {
          //process the command with the argument "add"
          $comandoController->updateDomain($dominioVo, 'adicionar');
      }
      catch (EppCommandsExceptions $e)
      {
           echo 'Exceção: ',  $e->getMessage();
      }
                    
    }
    //if the info command return results
    elseif ((!empty($hostNameInfo)) && (!empty($hostAddressInfo)))
    {
        //and those results are equal to those on the input fields
        if ( ($hostNameInfo == $hostNameInput) && ($hostAddressInfo == $hostAddressInput) )
        {
            return; //do nothing.
        }
        //if they are different and the input field values are NOT empty:
        elseif (!empty($hostNameInput) && (!empty($hostAddressInput)))
        {
           $dominioVo->setNsNome($hostNameInput);
           $dominioVo->setNsEndereco($hostAddressInput);
            $dominioVo->setNumeroProcesso($roidInfo);
        try
        {
            //also run a updateDomain command with the param "add" (adicionar) :)
            $comandoController->updateDomain($dominioVo, 'adicionar');
         }
         catch (EppCommandsExceptions $e)
         {
            echo 'Exceção: ',  $e->getMessage();
         }
     }
     //if they are different and the hostNameInput and hostAddressInput ARE empty
     elseif(empty($hostNameInput) && empty($hostAddressInput))
     {
         $dominioVo->setNsNome($hostNameInfo); //aqui usamos o name do info e não do input.
          $dominioVo->setNsEndereco($hostAddressInfo); //ibidem.
          $dominioVo->setNumeroProcesso($roidInfo);
          try
          {
	    //seems that they were filled in, and now they aren't. A command with a "remove" parameters will be used.
             $comandoController->updateDomain($dominioVo, 'remover');
           }
           catch (EppCommandsExceptions $e)
           {
               echo 'Exceção: ',  $e->getMessage();
            }
        }
     }
}

$comandoController->logout();
:blush:

:cool: Proud Newbie!

Márcio