ApiService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Class ApiService
  4. * User: Nessandro
  5. * Date: 2019-09-12
  6. * Time: 10:42
  7. * @package ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces
  8. */
  9. namespace ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces;
  10. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Exceptions\KerioServiceException;
  11. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Client;
  12. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Traits\ApiClientHandler;
  13. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Traits\ErrorHandler;
  14. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Traits\FormDataHandler;
  15. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Traits\ProcessStepHandler;
  16. use function ThurData\Servers\KerioEmail\Core\Helper\response;
  17. class ApiService
  18. {
  19. use ApiClientHandler;
  20. use FormDataHandler;
  21. use ProcessStepHandler;
  22. use ErrorHandler;
  23. /**
  24. * ApiService constructor.
  25. * @param Client|null $api
  26. */
  27. public function __construct(Client $api = null )
  28. {
  29. if($api)
  30. {
  31. $this->setApi($api);
  32. }
  33. }
  34. /**
  35. * configure service
  36. */
  37. protected function config()
  38. {
  39. //todo
  40. }
  41. /**
  42. * Check if service is valid
  43. *
  44. * @return bool
  45. */
  46. protected function isValid()
  47. {
  48. /**
  49. * check if api is set
  50. */
  51. if(!$this->api)
  52. {
  53. $this->setError('API Not Found');
  54. return false;
  55. }
  56. return true;
  57. }
  58. /**
  59. * main process of service
  60. *
  61. * @return mixed
  62. */
  63. protected function process()
  64. {
  65. //todo
  66. }
  67. /**
  68. * clear data after service
  69. */
  70. protected function clear()
  71. {
  72. }
  73. /**
  74. * Run process of service
  75. *
  76. * @return bool|void
  77. */
  78. public function run()
  79. {
  80. $this->config();
  81. if($result = $this->isValid())
  82. {
  83. $result = $this->process();
  84. }
  85. $this->clear();
  86. return $result;
  87. }
  88. /**
  89. *
  90. * @description catch exceptions
  91. * @return bool|string|void
  92. */
  93. public function safeRun()
  94. {
  95. try{
  96. return $this->run();
  97. }catch (\Exception $e){
  98. return $e->getMessage();
  99. }
  100. }
  101. /**
  102. * @description run if you want to return some error
  103. * @param string $message
  104. * @param int $code
  105. * @throws KerioServiceException
  106. */
  107. protected function throwError($message = 'Service error, contact ThurData to investigate this problem.', $code = 0)
  108. {
  109. throw new KerioServiceException($message, $code);
  110. }
  111. }