/src/plugins/orangehrmRecruitmentPlugin/Controller/PublicController/VacancyListRestController.php

https://github.com/orangehrm/OrangeHRM · PHP · 189 lines · 107 code · 17 blank · 65 comment · 1 complexity · 0c00a2a887ba7ea82c4bb78f18fbe23a MD5 · raw file

  1. <?php
  2. /**
  3. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  4. * all the essential functionalities required for any enterprise.
  5. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  6. *
  7. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program;
  16. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA
  18. */
  19. namespace OrangeHRM\Recruitment\Controller\PublicController;
  20. use OrangeHRM\Core\Api\CommonParams;
  21. use OrangeHRM\Core\Api\V2\Exception\NotImplementedException;
  22. use OrangeHRM\Core\Api\V2\Request;
  23. use OrangeHRM\Core\Api\V2\Response;
  24. use OrangeHRM\Core\Api\V2\Validator\Helpers\ValidationDecorator;
  25. use OrangeHRM\Core\Api\V2\Validator\ParamRule;
  26. use OrangeHRM\Core\Api\V2\Validator\ParamRuleCollection;
  27. use OrangeHRM\Core\Api\V2\Validator\Rule;
  28. use OrangeHRM\Core\Api\V2\Validator\Rules;
  29. use OrangeHRM\Core\Controller\PublicControllerInterface;
  30. use OrangeHRM\Core\Controller\Rest\V2\AbstractRestController;
  31. use OrangeHRM\Core\Dto\FilterParams;
  32. use OrangeHRM\Core\Exception\SearchParamException;
  33. use OrangeHRM\Core\Traits\Service\NormalizerServiceTrait;
  34. use OrangeHRM\ORM\ListSorter;
  35. use OrangeHRM\Recruitment\Api\Model\VacancyModel;
  36. use OrangeHRM\Recruitment\Dto\VacancySearchFilterParams;
  37. use OrangeHRM\Recruitment\Traits\Service\VacancyServiceTrait;
  38. class VacancyListRestController extends AbstractRestController implements PublicControllerInterface
  39. {
  40. use VacancyServiceTrait;
  41. use NormalizerServiceTrait;
  42. private const VACANCY_ID = 'vacancy.id';
  43. private const VACANCY_OFFSET = 'offset';
  44. private const VACANCY_LIMIT = 'limit';
  45. /**
  46. * @var ValidationDecorator|null
  47. */
  48. private ?ValidationDecorator $validationDecorator = null;
  49. /**
  50. * @param Request $request
  51. * @return Response
  52. * @throws SearchParamException
  53. */
  54. public function handleGetRequest(Request $request): Response
  55. {
  56. $offset = $request->getQuery()->get(self::VACANCY_OFFSET, FilterParams::DEFAULT_OFFSET);
  57. $limit = $request->getQuery()->get(self::VACANCY_LIMIT, FilterParams::DEFAULT_LIMIT);
  58. $vacancySearchFilterParams = new VacancySearchFilterParams();
  59. $vacancySearchFilterParams->setStatus(true);
  60. $vacancySearchFilterParams->setIsPublished(true);
  61. $vacancySearchFilterParams->setSortField(self::VACANCY_ID);
  62. $vacancySearchFilterParams->setSortOrder(ListSorter::DESCENDING);
  63. $vacancySearchFilterParams->setLimit($limit);
  64. $vacancySearchFilterParams->setOffset($offset);
  65. $vacancies = $this->getVacancyService()->getVacancyDao()->getVacancies($vacancySearchFilterParams);
  66. $count = $this->getVacancyService()->getVacancyDao()->getVacanciesCount($vacancySearchFilterParams);
  67. return new Response(
  68. $this->getNormalizerService()
  69. ->normalizeArray(VacancyModel::class, $vacancies),
  70. [CommonParams::PARAMETER_TOTAL => $count]
  71. );
  72. }
  73. /**
  74. * @param Request $request
  75. * @return Response
  76. * @throws NotImplementedException
  77. */
  78. public function handlePostRequest(Request $request): Response
  79. {
  80. throw $this->getNotImplementedException();
  81. }
  82. /**
  83. * @return NotImplementedException
  84. */
  85. private function getNotImplementedException(): NotImplementedException
  86. {
  87. return new NotImplementedException();
  88. }
  89. /**
  90. * @param Request $request
  91. * @return Response
  92. * @throws NotImplementedException
  93. */
  94. public function handlePutRequest(Request $request): Response
  95. {
  96. throw $this->getNotImplementedException();
  97. }
  98. /**
  99. * @param Request $request
  100. * @return Response
  101. * @throws NotImplementedException
  102. */
  103. public function handleDeleteRequest(Request $request): Response
  104. {
  105. throw $this->getNotImplementedException();
  106. }
  107. /**
  108. * @param Request $request
  109. * @return ParamRuleCollection|null
  110. */
  111. protected function initGetValidationRule(Request $request): ?ParamRuleCollection
  112. {
  113. return new ParamRuleCollection(
  114. $this->getValidationDecorator()->notRequiredParamRule(
  115. new ParamRule(
  116. CommonParams::PARAMETER_LIMIT,
  117. new Rule(Rules::ZERO_OR_POSITIVE), // Zero for not to limit results
  118. )
  119. ),
  120. $this->getValidationDecorator()->notRequiredParamRule(
  121. new ParamRule(
  122. CommonParams::PARAMETER_OFFSET,
  123. new Rule(Rules::ZERO_OR_POSITIVE)
  124. )
  125. ),
  126. $this->getValidationDecorator()->notRequiredParamRule(
  127. new ParamRule(
  128. CommonParams::PARAMETER_SORT_ORDER,
  129. new Rule(Rules::IN, [[ListSorter::ASCENDING, ListSorter::DESCENDING]])
  130. ),
  131. true
  132. )
  133. );
  134. }
  135. /**
  136. * @return ValidationDecorator
  137. */
  138. protected function getValidationDecorator(): ValidationDecorator
  139. {
  140. if (!$this->validationDecorator instanceof ValidationDecorator) {
  141. $this->validationDecorator = new ValidationDecorator();
  142. }
  143. return $this->validationDecorator;
  144. }
  145. /**
  146. * @param Request $request
  147. * @return ParamRuleCollection|null
  148. * @throws NotImplementedException
  149. */
  150. public function initPostValidationRule(Request $request): ?ParamRuleCollection
  151. {
  152. throw $this->getNotImplementedException();
  153. }
  154. /**
  155. * @param Request $request
  156. * @return ParamRuleCollection|null
  157. * @throws NotImplementedException
  158. */
  159. public function initPutValidationRule(Request $request): ?ParamRuleCollection
  160. {
  161. throw $this->getNotImplementedException();
  162. }
  163. /**
  164. * @param Request $request
  165. * @return ParamRuleCollection|null
  166. * @throws NotImplementedException
  167. */
  168. public function initDeleteValidationRule(Request $request): ?ParamRuleCollection
  169. {
  170. throw $this->getNotImplementedException();
  171. }
  172. }