ConsumersController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Application\Admin;
  4. use App\Infrastructure\Consumer\ConsumerRepository;
  5. use Psr\Http\Message\ResponseInterface;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. /**
  8. * Admin CRUD over consumers. Mirrors ReportersController's shape; the
  9. * differences are `policy_id` (FK validated against `policies`) and the
  10. * delete-on-active-tokens consideration: the api_tokens FK is CASCADE,
  11. * so dropping a consumer revokes its tokens automatically. We still do
  12. * a soft delete by default to match reporters.
  13. */
  14. final class ConsumersController
  15. {
  16. use AdminControllerSupport;
  17. public function __construct(private readonly ConsumerRepository $consumers)
  18. {
  19. }
  20. public function list(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
  21. {
  22. $page = self::pagination($request);
  23. $rows = $this->consumers->list($page['limit'], $page['offset']);
  24. $total = $this->consumers->count();
  25. return self::json($response, 200, [
  26. 'data' => array_map(static fn ($c) => $c->toArray(), $rows),
  27. 'page' => $page['page'],
  28. 'limit' => $page['limit'],
  29. 'total' => $total,
  30. ]);
  31. }
  32. /**
  33. * @param array{id: string} $args
  34. */
  35. public function show(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
  36. {
  37. $id = self::parseId($args['id']);
  38. if ($id === null) {
  39. return self::error($response, 404, 'not_found');
  40. }
  41. $consumer = $this->consumers->findById($id);
  42. if ($consumer === null) {
  43. return self::error($response, 404, 'not_found');
  44. }
  45. return self::json($response, 200, $consumer->toArray());
  46. }
  47. public function create(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
  48. {
  49. $body = self::jsonBody($request);
  50. $errors = [];
  51. $name = isset($body['name']) && is_string($body['name']) ? trim($body['name']) : '';
  52. if ($name === '' || strlen($name) > 128) {
  53. $errors['name'] = 'required, 1–128 chars';
  54. }
  55. $description = null;
  56. if (array_key_exists('description', $body)) {
  57. if ($body['description'] !== null && !is_string($body['description'])) {
  58. $errors['description'] = 'must be string or null';
  59. } else {
  60. $description = $body['description'];
  61. }
  62. }
  63. $policyId = null;
  64. if (!array_key_exists('policy_id', $body) || !is_int($body['policy_id'])) {
  65. // Accept numeric string too — JSON usually has int but be lenient.
  66. if (isset($body['policy_id']) && is_string($body['policy_id']) && ctype_digit($body['policy_id'])) {
  67. $policyId = (int) $body['policy_id'];
  68. } else {
  69. $errors['policy_id'] = 'required, integer';
  70. }
  71. } else {
  72. $policyId = $body['policy_id'];
  73. }
  74. if ($policyId !== null && $policyId > 0 && !$this->consumers->policyExists($policyId)) {
  75. $errors['policy_id'] = 'unknown policy';
  76. }
  77. if ($errors === [] && $this->consumers->findByName($name) !== null) {
  78. $errors['name'] = 'already exists';
  79. }
  80. if ($errors !== []) {
  81. return self::validationFailed($response, $errors);
  82. }
  83. /** @var int $policyId */
  84. $id = $this->consumers->create($name, $description, $policyId, self::actingUserId($request));
  85. $created = $this->consumers->findById($id);
  86. if ($created === null) {
  87. return self::error($response, 500, 'create_failed');
  88. }
  89. return self::json($response, 201, $created->toArray());
  90. }
  91. /**
  92. * @param array{id: string} $args
  93. */
  94. public function update(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
  95. {
  96. $id = self::parseId($args['id']);
  97. if ($id === null) {
  98. return self::error($response, 404, 'not_found');
  99. }
  100. $existing = $this->consumers->findById($id);
  101. if ($existing === null) {
  102. return self::error($response, 404, 'not_found');
  103. }
  104. $body = self::jsonBody($request);
  105. $errors = [];
  106. $fields = [];
  107. if (array_key_exists('name', $body)) {
  108. if (!is_string($body['name']) || trim($body['name']) === '' || strlen(trim($body['name'])) > 128) {
  109. $errors['name'] = 'required, 1–128 chars';
  110. } else {
  111. $name = trim($body['name']);
  112. $other = $this->consumers->findByName($name);
  113. if ($other !== null && $other->id !== $id) {
  114. $errors['name'] = 'already exists';
  115. } else {
  116. $fields['name'] = $name;
  117. }
  118. }
  119. }
  120. if (array_key_exists('description', $body)) {
  121. if ($body['description'] !== null && !is_string($body['description'])) {
  122. $errors['description'] = 'must be string or null';
  123. } else {
  124. $fields['description'] = $body['description'];
  125. }
  126. }
  127. if (array_key_exists('policy_id', $body)) {
  128. $newPolicy = null;
  129. if (is_int($body['policy_id'])) {
  130. $newPolicy = $body['policy_id'];
  131. } elseif (is_string($body['policy_id']) && ctype_digit($body['policy_id'])) {
  132. $newPolicy = (int) $body['policy_id'];
  133. }
  134. if ($newPolicy === null || $newPolicy <= 0) {
  135. $errors['policy_id'] = 'must be positive integer';
  136. } elseif (!$this->consumers->policyExists($newPolicy)) {
  137. $errors['policy_id'] = 'unknown policy';
  138. } else {
  139. $fields['policy_id'] = $newPolicy;
  140. }
  141. }
  142. if (array_key_exists('is_active', $body)) {
  143. if (!is_bool($body['is_active'])) {
  144. $errors['is_active'] = 'must be boolean';
  145. } else {
  146. $fields['is_active'] = $body['is_active'] ? 1 : 0;
  147. }
  148. }
  149. if ($errors !== []) {
  150. return self::validationFailed($response, $errors);
  151. }
  152. $this->consumers->update($id, $fields);
  153. $updated = $this->consumers->findById($id);
  154. if ($updated === null) {
  155. return self::error($response, 500, 'update_failed');
  156. }
  157. return self::json($response, 200, $updated->toArray());
  158. }
  159. /**
  160. * @param array{id: string} $args
  161. */
  162. public function delete(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
  163. {
  164. $id = self::parseId($args['id']);
  165. if ($id === null) {
  166. return self::error($response, 404, 'not_found');
  167. }
  168. $existing = $this->consumers->findById($id);
  169. if ($existing === null) {
  170. return self::error($response, 404, 'not_found');
  171. }
  172. $this->consumers->softDelete($id);
  173. return $response->withStatus(204);
  174. }
  175. private static function parseId(string $raw): ?int
  176. {
  177. return preg_match('/^[1-9][0-9]*$/', $raw) === 1 ? (int) $raw : null;
  178. }
  179. }