Api.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra;
  3. use ModulesGarden\Servers\ZimbraEmail\App\Enums\Zimbra;
  4. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Exceptions\ZimbraApiException;
  5. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Interfaces\ClientInterface;
  6. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Client as SoapClient;
  7. use ModulesGarden\Servers\ZimbraEmail\App\Traits\ParamsHandler;
  8. use ModulesGarden\Servers\ZimbraEmail\App\Traits\StorageTrait;
  9. /**
  10. *
  11. * Created by PhpStorm.
  12. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  13. * Date: 27.08.19
  14. * Time: 15:05
  15. * Class ZimbraClientConnection
  16. * @property SoapClient $soap
  17. */
  18. class Api
  19. {
  20. use StorageTrait;
  21. use ParamsHandler;
  22. /**
  23. * Api constructor.
  24. * @param array $params
  25. */
  26. public function __construct($params = [])
  27. {
  28. $this->setParams($params);
  29. }
  30. /**
  31. * @param $name
  32. * @return ClientInterface
  33. * @throws \Exception
  34. */
  35. public function __get($name)
  36. {
  37. /**
  38. * check if class is in storage
  39. */
  40. if(!$this->getStorage()->exists(strtolower($name)))
  41. {
  42. /**
  43. * prepare class namespace
  44. */
  45. $class = '\\ModulesGarden\\Servers\\ZimbraEmail\\App\\Libs\\Zimbra\\Components\\Api\\'.ucfirst($name).'\\Client';
  46. /**
  47. * check if class exists or throw error
  48. */
  49. if(!class_exists($class))
  50. {
  51. throw new ZimbraApiException([
  52. 'message' => 'Class `'.$class.'`` does not exists',
  53. 'debugCode' => ZimbraApiException::UNSUPORTED_CLIENT]);
  54. }
  55. /**
  56. *
  57. * create new client class
  58. * prepare server parameters
  59. */
  60. $hostname = $this->params['serverhostname'] ? $this->params['serverhostname'] : $this->params['serverip'];
  61. $port = $this->params['serversecure'] === 'on' ? Zimbra::SECURE_PORT : Zimbra::PORT;
  62. $port = $this->params['serverport'] ? $this->params['serverport'] : $port;
  63. $username = $this->params['serverusername'];
  64. $pw = html_entity_decode($this->params['serverpassword']);
  65. $auth = $this->params['authtype'] ? $this->params['authtype'] : 'admin';
  66. $client = new $class($hostname, $port, $username, $pw, $auth);
  67. /**
  68. *
  69. * throw exception if class is not implemented
  70. */
  71. if(!($client instanceof ClientInterface))
  72. {
  73. throw new ZimbraApiException([
  74. 'message' => "Provided client type `{$class}` is not supported",
  75. 'debugCode' => ZimbraApiException::UNSUPORTED_CLIENT]);
  76. }
  77. /**
  78. *
  79. * add class to storage
  80. */
  81. $this->getStorage()->set(strtolower($name), $client);
  82. }else{
  83. /**
  84. *
  85. * get client from storage
  86. */
  87. $client = $this->getStorage()->get(strtolower($name));
  88. }
  89. /**
  90. *
  91. */
  92. return $client;
  93. }
  94. }