<?php

namespace ModulesGarden\Servers\ZimbraEmail\App\Http\Actions;


use ModulesGarden\Servers\ZimbraEmail\App\Enums\Response;
use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\ZimbraEmail\App\Traits\ExtensionsCheckerTrait;
use ModulesGarden\Servers\ZimbraEmail\Core\App\Controllers\Instances\AddonController;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 09.09.19
 * Time: 15:15
 * Class UnsuspendAccount
 */
class UnsuspendAccount extends AddonController
{

    use ExtensionsCheckerTrait;

    public function execute($params = null)
    {
        try{

            /**
             * check if extensions are installed
             */
            $this->checkExtensionOrThrowError();

            /**
             * run zimbra service
             */
            $result =  $this->zimbraRunService($params);

            /**
             * return result
             */
            return $result;
        }catch (\Exception $ex)
        {
            /**
             * return some crit error
             */
            return $ex->getMessage();
        }

    }

    /**
     * @param null $params
     * @return mixed|string
     */
    protected function zimbraRunService($params = null)
    {
        $service = (new ZimbraManager())
            ->getApiByServer($params['serverid'])
            ->soap
            ->service()
            ->unsuspendDomain()
            ->setFormData($params)
        ;

        $result = $service->run();

        if(!$result)
        {
            return $service->getError();
        }

        /**
         * return success response
         */
        return Response::SUCCESS;
    }
}