<?php

namespace ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Delete;


use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\ApiService;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\AccountAlias;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * 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->getLastResult()->getLastErrorCode());
            return false;
        }

        return $result;

    }
}