DeleteDomain.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Delete;
  3. use ModulesGarden\Servers\KerioEmail\App\Enums\Kerio;
  4. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Exceptions\KerioApiException;
  5. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Exceptions\KerioServiceException;
  6. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Enums\ApiErrorCodes;
  7. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces\ApiService;
  8. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\DistributionList;
  9. use ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain;
  10. /**
  11. *
  12. * Created by PhpStorm.
  13. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  14. * Date: 10.10.19
  15. * Time: 14:41
  16. * Class DeleteDomain
  17. */
  18. class DeleteDomain extends ApiService
  19. {
  20. protected $domain;
  21. /**
  22. * @return bool
  23. */
  24. public function isValid()
  25. {
  26. if(!$this->formData['domain'])
  27. {
  28. $this->setError('Domain not found');
  29. return false;
  30. }
  31. return true;
  32. }
  33. /**
  34. * @return mixed|void
  35. */
  36. public function process()
  37. {
  38. $this->setDomainAsActiveIfRequired();
  39. $this->removeLists();
  40. $this->removeAccounts();
  41. $this->removeDomain();
  42. if ($this->getError())
  43. {
  44. return false;
  45. }
  46. return true;
  47. }
  48. protected function setDomainAsActiveIfRequired()
  49. {
  50. $domain = $this->getOrLoadDomain();
  51. $domain->setAttrs([
  52. Domain::ATTR_DOMAIN_STATUS => Kerio::ACC_STATUS_ACTIVE,
  53. Domain::ATTR_MAIL_STATUS => Kerio::ENABLED,
  54. ]);
  55. $response = $this->api->domain->update($domain);
  56. return $this;
  57. }
  58. /**
  59. * remove distribution list
  60. *
  61. * @return bool
  62. */
  63. protected function removeLists()
  64. {
  65. /**
  66. * get lists
  67. */
  68. $lists = $this->api->repository()->lists->getAllDistributionListsByDomain($this->formData['domain']);
  69. /**
  70. * remove each list
  71. */
  72. foreach($lists as $list)
  73. {
  74. /* @var $list DistributionList */
  75. $result = $this->api->distributionList->delete($list);
  76. if($this->api->distributionList->getError())
  77. {
  78. $this->throwError($this->api->distributionList->getError());
  79. }
  80. }
  81. return true;
  82. }
  83. /**
  84. *
  85. * @return bool
  86. *
  87. */
  88. protected function removeAccounts()
  89. {
  90. /**
  91. * get accounts from Kerio
  92. */
  93. $accounts = $this->api->repository()->accounts->getByDomainName($this->formData['domain']);
  94. /**
  95. * remove each account
  96. */
  97. foreach($accounts as $account)
  98. {
  99. $result = $this->api->account->delete($account);
  100. if($this->api->account->getError())
  101. {
  102. $this->throwError($this->api->account->getError());
  103. }
  104. }
  105. return true;
  106. }
  107. /**
  108. * remove domain from API
  109. *
  110. * @return bool
  111. */
  112. protected function removeDomain()
  113. {
  114. /**
  115. *
  116. * get domain from kerio
  117. */
  118. $domain = $this->getOrLoadDomain();
  119. /**
  120. * remove domain
  121. */
  122. $result = $this->api->domain->delete($domain);
  123. if(!$result && ApiErrorCodes::DOMAIN_DOES_NOT_EXISTS !== $this->api->domain->getLastResult()->getLastErrorCode())
  124. {
  125. $this->setError($this->api->domain->getLastResult()->getLastErrorCode());
  126. return false;
  127. }
  128. return true;
  129. }
  130. /**
  131. *
  132. * @return \ModulesGarden\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Domain
  133. */
  134. protected function getOrLoadDomain()
  135. {
  136. if(!$this->domain)
  137. {
  138. $this->domain = $this->api->repository()->domains->getByName($this->formData['domain']);
  139. }
  140. if ($this->api->repository()->domains->getError())
  141. {
  142. $this->throwError($this->api->repository()->domains->getError());
  143. }
  144. return $this->domain;
  145. }
  146. }