| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace ModulesGarden\Servers\KerioEmail\App\Libs\Kerio;
- use ModulesGarden\Servers\KerioEmail\App\Enums\Kerio;
- use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Exceptions\KerioApiException;
- use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Interfaces\ClientInterface;
- use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Client as SoapClient;
- use ModulesGarden\Servers\KerioEmail\App\Traits\ParamsHandler;
- use ModulesGarden\Servers\KerioEmail\App\Traits\StorageTrait;
- /**
- *
- * Created by PhpStorm.
- * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
- * Date: 27.08.19
- * Time: 15:05
- * Class KerioClientConnection
- * @property SoapClient $soap
- */
- class Api
- {
- use StorageTrait;
- use ParamsHandler;
- /**
- * Api constructor.
- * @param array $params
- */
- public function __construct($params = [])
- {
- $this->setParams($params);
- }
- /**
- * @param $name
- * @return ClientInterface
- * @throws \Exception
- */
- public function __get($name)
- {
- /**
- * check if class is in storage
- */
- if(!$this->getStorage()->exists(strtolower($name)))
- {
- /**
- * prepare class namespace
- */
- $class = '\\ModulesGarden\\Servers\\KerioEmail\\App\\Libs\\Kerio\\Components\\Api\\'.ucfirst($name).'\\Client';
- /**
- * check if class exists or throw error
- */
- if(!class_exists($class))
- {
- throw new KerioApiException([
- 'message' => 'Class `'.$class.'`` does not exists',
- 'debugCode' => KerioApiException::UNSUPORTED_CLIENT]);
- }
- /**
- *
- * create new client class
- * prepare server parameters
- */
- $hostname = $this->params['serverhostname'] ? $this->params['serverhostname'] : $this->params['serverip'];
- $port = $this->params['serversecure'] === 'on' ? Kerio::SECURE_PORT : Kerio::PORT;
- $port = $this->params['serverport'] ? $this->params['serverport'] : $port;
- $username = $this->params['serverusername'];
- $pw = html_entity_decode($this->params['serverpassword']);
- $auth = $this->params['authtype'] ? $this->params['authtype'] : 'admin';
- $client = new $class($hostname, $port, $username, $pw, $auth);
- /**
- *
- * throw exception if class is not implemented
- */
- if(!($client instanceof ClientInterface))
- {
- throw new KerioApiException([
- 'message' => "Provided client type `{$class}` is not supported",
- 'debugCode' => KerioApiException::UNSUPORTED_CLIENT]);
- }
- /**
- *
- * add class to storage
- */
- $this->getStorage()->set(strtolower($name), $client);
- }else{
- /**
- *
- * get client from storage
- */
- $client = $this->getStorage()->get(strtolower($name));
- }
- /**
- *
- */
- return $client;
- }
- }
|