<?php

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


use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\AbstractApiClient;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\AbstractConnection;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\Accounts;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\Ressources;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\Server;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\DistributionLists;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\Domains;
use ThurData\Servers\KerioEmail\App\Traits\StorageTrait;

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * Date: 09.09.19
 * Time: 09:37
 * Class Repository
 * @property ClassOfServices $cos
 * @property Domains $domains
 * @property Accounts $accounts
 * @property DistributionLists $lists
 */
class Repository extends AbstractApiClient
{
    use StorageTrait;

    public function __get($name)
    {
        $name = strtolower($name);
        $exists = $this->getStorage()->exists($name);

        if(!$exists && method_exists($this, $name))
        {
            $action = $this->$name();
            $this->getStorage()->set($name, $action);
        }elseif(!$exists)
        {

        }elseif($exists){

            $action = $this->getStorage()->get($name);
        }else{
            throw new \Exception("Repository `{$name}` not found");
        }

        return $action;
    }

    /**
     * @return Server
     */
    public function server()
    {
        return new Server($this->getClient());
    }

    /**
     * @return ClassOfServices
     */
    public function cos()
    {
        return new ClassOfServices($this->getClient());
    }

    /**
     * @return Domains
     */
    public function domains()
    {
        return new Domains($this->getClient());

    }

    /**
     * @return Accounts
     */
    public function accounts()
    {
        return new Accounts($this->getClient());
    }

    /**
     * @return Ressources
     */
    public function ressources()
    {
        return new Ressources($this->getClient());
    }

    /**
     *
     */
    public function lists()
    {
        return new DistributionLists($this->getClient());
    }
}