AdminClient.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\ApiClient;
  4. use App\ApiClient\DTOs\DashboardStatsDto;
  5. use App\ApiClient\DTOs\IpDetailDto;
  6. use App\ApiClient\DTOs\IpListDto;
  7. use App\ApiClient\DTOs\UserDto;
  8. /**
  9. * Wraps the api's `/api/v1/admin/*` endpoints. Calls go out with the
  10. * service token plus `X-Acting-User-Id` from the current session — the
  11. * api uses that to resolve the impersonated user's role and enforce
  12. * RBAC.
  13. *
  14. * For most CRUD endpoints (manual_blocks, allowlist, policies,
  15. * reporters, consumers, tokens, categories) we return raw associative
  16. * arrays mirroring the api's JSON shape. Templates bind onto these
  17. * directly. The richer DTO pattern is reserved for endpoints whose
  18. * response shape benefits from a typed accessor (`UserDto`, the
  19. * IP-detail / dashboard payloads).
  20. *
  21. * Throws the typed `ApiException` subclasses on non-2xx; controllers
  22. * catch them to render validation messages or "API unreachable" states.
  23. */
  24. final class AdminClient
  25. {
  26. public function __construct(private readonly ApiClient $api)
  27. {
  28. }
  29. // ---- identity ----
  30. public function getMe(int $actingUserId): UserDto
  31. {
  32. $payload = $this->api->request('GET', '/api/v1/admin/me', [], $actingUserId);
  33. return UserDto::fromArray($payload);
  34. }
  35. // ---- IPs / dashboard (M09) ----
  36. /**
  37. * @param array<string, mixed> $filters
  38. */
  39. public function searchIps(int $actingUserId, array $filters, int $page = 1, int $pageSize = 25): IpListDto
  40. {
  41. $query = ['page' => $page, 'page_size' => $pageSize];
  42. foreach (['q', 'category', 'min_score', 'max_score', 'country', 'asn', 'status'] as $key) {
  43. if (isset($filters[$key]) && $filters[$key] !== '' && $filters[$key] !== null) {
  44. $query[$key] = $filters[$key];
  45. }
  46. }
  47. $payload = $this->api->request('GET', '/api/v1/admin/ips', ['query' => $query], $actingUserId);
  48. return IpListDto::fromArray($payload);
  49. }
  50. public function getIp(int $actingUserId, string $ip): IpDetailDto
  51. {
  52. $payload = $this->api->request('GET', '/api/v1/admin/ips/' . rawurlencode($ip), [], $actingUserId);
  53. return IpDetailDto::fromArray($payload);
  54. }
  55. public function getDashboardStats(int $actingUserId): DashboardStatsDto
  56. {
  57. $payload = $this->api->request('GET', '/api/v1/admin/stats/dashboard', [], $actingUserId);
  58. return DashboardStatsDto::fromArray($payload);
  59. }
  60. // ---- manual blocks (M10) ----
  61. /**
  62. * @return array<string, mixed>
  63. */
  64. public function listManualBlocks(int $actingUserId, ?string $kind = null): array
  65. {
  66. $query = ['limit' => 200];
  67. if ($kind !== null && $kind !== '') {
  68. $query['kind'] = $kind;
  69. }
  70. return $this->api->request('GET', '/api/v1/admin/manual-blocks', ['query' => $query], $actingUserId);
  71. }
  72. /**
  73. * @param array<string, mixed> $body
  74. * @return array<string, mixed>
  75. */
  76. public function createManualBlock(int $actingUserId, array $body): array
  77. {
  78. return $this->api->request('POST', '/api/v1/admin/manual-blocks', ['json' => $body], $actingUserId);
  79. }
  80. public function deleteManualBlock(int $actingUserId, int $id): void
  81. {
  82. $this->api->request('DELETE', '/api/v1/admin/manual-blocks/' . $id, [], $actingUserId);
  83. }
  84. // ---- allowlist (M10) ----
  85. /**
  86. * @return array<string, mixed>
  87. */
  88. public function listAllowlist(int $actingUserId, ?string $kind = null): array
  89. {
  90. $query = ['limit' => 200];
  91. if ($kind !== null && $kind !== '') {
  92. $query['kind'] = $kind;
  93. }
  94. return $this->api->request('GET', '/api/v1/admin/allowlist', ['query' => $query], $actingUserId);
  95. }
  96. /**
  97. * @param array<string, mixed> $body
  98. * @return array<string, mixed>
  99. */
  100. public function createAllowlist(int $actingUserId, array $body): array
  101. {
  102. return $this->api->request('POST', '/api/v1/admin/allowlist', ['json' => $body], $actingUserId);
  103. }
  104. public function deleteAllowlist(int $actingUserId, int $id): void
  105. {
  106. $this->api->request('DELETE', '/api/v1/admin/allowlist/' . $id, [], $actingUserId);
  107. }
  108. // ---- policies (M10) ----
  109. /**
  110. * @return array<string, mixed>
  111. */
  112. public function listPolicies(int $actingUserId): array
  113. {
  114. return $this->api->request('GET', '/api/v1/admin/policies', [], $actingUserId);
  115. }
  116. /**
  117. * @return array<string, mixed>
  118. */
  119. public function getPolicy(int $actingUserId, int $id): array
  120. {
  121. return $this->api->request('GET', '/api/v1/admin/policies/' . $id, [], $actingUserId);
  122. }
  123. /**
  124. * @param array<string, mixed> $body
  125. * @return array<string, mixed>
  126. */
  127. public function createPolicy(int $actingUserId, array $body): array
  128. {
  129. return $this->api->request('POST', '/api/v1/admin/policies', ['json' => $body], $actingUserId);
  130. }
  131. /**
  132. * @param array<string, mixed> $body
  133. * @return array<string, mixed>
  134. */
  135. public function updatePolicy(int $actingUserId, int $id, array $body): array
  136. {
  137. return $this->api->request('PATCH', '/api/v1/admin/policies/' . $id, ['json' => $body], $actingUserId);
  138. }
  139. public function deletePolicy(int $actingUserId, int $id): void
  140. {
  141. $this->api->request('DELETE', '/api/v1/admin/policies/' . $id, [], $actingUserId);
  142. }
  143. /**
  144. * @return array<string, mixed>
  145. */
  146. public function previewPolicy(int $actingUserId, int $id): array
  147. {
  148. return $this->api->request('GET', '/api/v1/admin/policies/' . $id . '/preview', [], $actingUserId);
  149. }
  150. // ---- reporters (M10) ----
  151. /**
  152. * @return array<string, mixed>
  153. */
  154. public function listReporters(int $actingUserId): array
  155. {
  156. return $this->api->request('GET', '/api/v1/admin/reporters', ['query' => ['limit' => 200]], $actingUserId);
  157. }
  158. /**
  159. * @return array<string, mixed>
  160. */
  161. public function getReporter(int $actingUserId, int $id): array
  162. {
  163. return $this->api->request('GET', '/api/v1/admin/reporters/' . $id, [], $actingUserId);
  164. }
  165. /**
  166. * @param array<string, mixed> $body
  167. * @return array<string, mixed>
  168. */
  169. public function createReporter(int $actingUserId, array $body): array
  170. {
  171. return $this->api->request('POST', '/api/v1/admin/reporters', ['json' => $body], $actingUserId);
  172. }
  173. /**
  174. * @param array<string, mixed> $body
  175. * @return array<string, mixed>
  176. */
  177. public function updateReporter(int $actingUserId, int $id, array $body): array
  178. {
  179. return $this->api->request('PATCH', '/api/v1/admin/reporters/' . $id, ['json' => $body], $actingUserId);
  180. }
  181. public function deleteReporter(int $actingUserId, int $id): void
  182. {
  183. $this->api->request('DELETE', '/api/v1/admin/reporters/' . $id, [], $actingUserId);
  184. }
  185. // ---- consumers (M10) ----
  186. /**
  187. * @return array<string, mixed>
  188. */
  189. public function listConsumers(int $actingUserId): array
  190. {
  191. return $this->api->request('GET', '/api/v1/admin/consumers', ['query' => ['limit' => 200]], $actingUserId);
  192. }
  193. /**
  194. * @return array<string, mixed>
  195. */
  196. public function getConsumer(int $actingUserId, int $id): array
  197. {
  198. return $this->api->request('GET', '/api/v1/admin/consumers/' . $id, [], $actingUserId);
  199. }
  200. /**
  201. * @param array<string, mixed> $body
  202. * @return array<string, mixed>
  203. */
  204. public function createConsumer(int $actingUserId, array $body): array
  205. {
  206. return $this->api->request('POST', '/api/v1/admin/consumers', ['json' => $body], $actingUserId);
  207. }
  208. /**
  209. * @param array<string, mixed> $body
  210. * @return array<string, mixed>
  211. */
  212. public function updateConsumer(int $actingUserId, int $id, array $body): array
  213. {
  214. return $this->api->request('PATCH', '/api/v1/admin/consumers/' . $id, ['json' => $body], $actingUserId);
  215. }
  216. public function deleteConsumer(int $actingUserId, int $id): void
  217. {
  218. $this->api->request('DELETE', '/api/v1/admin/consumers/' . $id, [], $actingUserId);
  219. }
  220. // ---- tokens (M10) ----
  221. /**
  222. * @return array<string, mixed>
  223. */
  224. public function listTokens(int $actingUserId): array
  225. {
  226. return $this->api->request('GET', '/api/v1/admin/tokens', ['query' => ['limit' => 200]], $actingUserId);
  227. }
  228. /**
  229. * @param array<string, mixed> $body
  230. * @return array<string, mixed>
  231. */
  232. public function createToken(int $actingUserId, array $body): array
  233. {
  234. return $this->api->request('POST', '/api/v1/admin/tokens', ['json' => $body], $actingUserId);
  235. }
  236. public function deleteToken(int $actingUserId, int $id): void
  237. {
  238. $this->api->request('DELETE', '/api/v1/admin/tokens/' . $id, [], $actingUserId);
  239. }
  240. // ---- categories (M10) ----
  241. /**
  242. * @return array<string, mixed>
  243. */
  244. public function listCategories(int $actingUserId): array
  245. {
  246. return $this->api->request('GET', '/api/v1/admin/categories', [], $actingUserId);
  247. }
  248. /**
  249. * @return array<string, mixed>
  250. */
  251. public function getCategory(int $actingUserId, int $id): array
  252. {
  253. return $this->api->request('GET', '/api/v1/admin/categories/' . $id, [], $actingUserId);
  254. }
  255. /**
  256. * @param array<string, mixed> $body
  257. * @return array<string, mixed>
  258. */
  259. public function createCategory(int $actingUserId, array $body): array
  260. {
  261. return $this->api->request('POST', '/api/v1/admin/categories', ['json' => $body], $actingUserId);
  262. }
  263. /**
  264. * @param array<string, mixed> $body
  265. * @return array<string, mixed>
  266. */
  267. public function updateCategory(int $actingUserId, int $id, array $body): array
  268. {
  269. return $this->api->request('PATCH', '/api/v1/admin/categories/' . $id, ['json' => $body], $actingUserId);
  270. }
  271. public function deleteCategory(int $actingUserId, int $id): void
  272. {
  273. $this->api->request('DELETE', '/api/v1/admin/categories/' . $id, [], $actingUserId);
  274. }
  275. }