<?php

namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Services\Delete;


use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Interfaces\ApiService;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\AccountAlias;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 18.09.19
 * Time: 13:11
 * Class DeleteAccountAlias
 */
class DeleteAccountAlias extends ApiService
{
    public function isValid()
    {
        /**
         * check if alias is provided
         */
        if(!$this->formData['alias'])
        {
            $this->setError('Alias can not be deleted. Invalid alias.');
            return false;
        }

        /**
         * check if mailbox data is provided
         */
        if(!$this->formData['id'])
        {
            $this->setError('Alias can not be deleted. Invalid account.');
            return false;
        }

        return true;
    }

    /**
     * @return bool|mixed
     */
    public function process()
    {
        /**
         * prepare model
         */
        $aliasAccount = new AccountAlias();
        $aliasAccount->setAlias($this->formData['alias']);
        $aliasAccount->setAccountId($this->formData['id']);

        /**
         * delete alias from API
         */
        $result = $this->api->account->deleteAlias($aliasAccount);

        /**
         * problem with create account
         */
        if(!$result)
        {
            $this->setError($this->api->account->getError());
            return false;
        }

        return $result;

    }
}