<?php

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


use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\AbstractApiClient;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\AbstractRepository;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Account;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 10.09.19
 * Time: 08:40
 * Class DistributionList
 */
class DistributionLists extends AbstractRepository
{

    /**
     * @param $domainName
     * @return mixed
     */
    public function getAllDistributionListsByDomain($domainName)
    {
        $domain = new Domain();
        $domain->setName($domainName);

        $result = $this->getClient()->distributionList->getAllDistributionLists($domain);

        $accounts = $result->getResponseBody()['GETALLDISTRIBUTIONLISTSRESPONSE']['DL'];

        /**
         * API return one or araay with accounts
         */
        if(isset($accounts['NAME']))
        {
            $tmpAccount =  new DistributionList($accounts);
            $tmp[$tmpAccount->getId()] = $tmpAccount;
        }else{
            foreach($accounts as $account)
            {
                $tmpAccount =  new DistributionList($account);
                $tmp[$tmpAccount->getId()] = $tmpAccount;
            }
        }

        return $tmp;
    }

    /**
     * @param $id
     * @return bool|DistributionList
     */
    public function getDistributionListBydId($id)
    {
        $list = new DistributionList();
        $list->setId($id);

        return $this->getClient()->distributionList->read($list);
    }

}