API.php
6 months ago
AbstractListingEndpoint.php
3 weeks ago
ApiException.php
2 months ago
Endpoint.php
3 months ago
EndpointContainer.php
3 years ago
ErrorResponse.php
3 years ago
Exception.php
3 years ago
ListingRequestValidationTrait.php
1 month ago
Request.php
3 years ago
Response.php
1 year ago
index.php
3 years ago
AbstractListingEndpoint.php
188 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\API\REST; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Listing\Handler as ListingHandler; |
| 9 | use MailPoet\Listing\ListingDefinition; |
| 10 | use MailPoet\Listing\ListingRepository; |
| 11 | use MailPoet\Validator\Builder; |
| 12 | |
| 13 | /** |
| 14 | * Base class for REST endpoints that expose a MailPoet listing |
| 15 | * (search, sort, filter, pagination) via the shared |
| 16 | * {@see ListingRepository} infrastructure. |
| 17 | * |
| 18 | * Concrete subclasses only need to supply the repository and the |
| 19 | * per-row response mapper. The request schema, parameter parsing, |
| 20 | * and response shape (`{items, meta:{count, pages}, filters, groups}`) |
| 21 | * are standardized so all DataViews-backed listings in the admin UI |
| 22 | * consume the same contract. |
| 23 | */ |
| 24 | abstract class AbstractListingEndpoint extends Endpoint { |
| 25 | public const DEFAULT_PER_PAGE = 20; |
| 26 | public const MAX_PER_PAGE = 100; |
| 27 | // Sentinel cap on `page`. Keeps `(page-1) * per_page` from producing |
| 28 | // huge OFFSETs that would push MySQL through millions of skipped rows |
| 29 | // when a client (or fuzzer) sends `page=999999999`. |
| 30 | public const MAX_PAGE = 100000; |
| 31 | |
| 32 | /** @var ListingHandler */ |
| 33 | private $listingHandler; |
| 34 | |
| 35 | public function __construct( |
| 36 | ListingHandler $listingHandler |
| 37 | ) { |
| 38 | $this->listingHandler = $listingHandler; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Subclasses MUST override `checkPermissions()` to declare a real |
| 43 | * capability. The inherited default from `Endpoint::checkPermissions()` |
| 44 | * checks a non-existent `admin` capability and would fail closed but |
| 45 | * silently. PHP does not let us redeclare the parent method as `abstract` |
| 46 | * here without breaking other consumers, so this is enforced by review + |
| 47 | * tests instead of the type system. |
| 48 | */ |
| 49 | public function handle(Request $request): Response { |
| 50 | $definition = $this->buildDefinition($request); |
| 51 | $repository = $this->getListingRepository(); |
| 52 | |
| 53 | $rows = $repository->getData($definition); |
| 54 | $countAndGroups = $this->getCountAndGroups($repository, $definition); |
| 55 | $count = $countAndGroups['count']; |
| 56 | $perPage = $definition->getLimit() ?: self::DEFAULT_PER_PAGE; |
| 57 | $pages = $count === 0 ? 0 : (int)ceil($count / max(1, $perPage)); |
| 58 | |
| 59 | return new Response([ |
| 60 | 'items' => $this->buildItems($rows, $definition), |
| 61 | 'meta' => [ |
| 62 | 'count' => $count, |
| 63 | 'pages' => $pages, |
| 64 | ], |
| 65 | 'filters' => $repository->getFilters($definition), |
| 66 | 'groups' => $countAndGroups['groups'], |
| 67 | ]); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns `['count' => int, 'groups' => array]`. Split into a hook so an |
| 72 | * endpoint whose group counts already include the current group's count can |
| 73 | * compute both from a single query and skip the separate getCount(). The |
| 74 | * default keeps the two independent queries, unchanged for every other |
| 75 | * listing. |
| 76 | * |
| 77 | * @return array{count: int, groups: array<int, array<string, mixed>>} |
| 78 | */ |
| 79 | protected function getCountAndGroups(ListingRepository $repository, ListingDefinition $definition): array { |
| 80 | return [ |
| 81 | 'count' => $repository->getCount($definition), |
| 82 | 'groups' => $repository->getGroups($definition), |
| 83 | ]; |
| 84 | } |
| 85 | |
| 86 | public static function getRequestSchema(): array { |
| 87 | return [ |
| 88 | 'page' => Builder::integer(), |
| 89 | 'per_page' => Builder::integer(), |
| 90 | 'orderby' => Builder::string(), |
| 91 | 'order' => Builder::string(), |
| 92 | 'sort_by' => Builder::string(), |
| 93 | 'sort_order' => Builder::string(), |
| 94 | 'search' => Builder::string(), |
| 95 | 'group' => Builder::string(), |
| 96 | 'filter' => Builder::object(), |
| 97 | ]; |
| 98 | } |
| 99 | |
| 100 | abstract protected function getListingRepository(): ListingRepository; |
| 101 | |
| 102 | /** |
| 103 | * @param mixed[] $rows Rows returned by {@see ListingRepository::getData()}. |
| 104 | * @param ListingDefinition $definition Parsed request — exposed so subclasses |
| 105 | * can branch on filter/group when shaping items without stashing per-request |
| 106 | * state on the (shared) endpoint instance. |
| 107 | * @return array<int, array<string, mixed>> Items ready to be serialized. |
| 108 | */ |
| 109 | abstract protected function buildItems(array $rows, ListingDefinition $definition): array; |
| 110 | |
| 111 | protected function getDefaultSortBy(): string { |
| 112 | return 'id'; |
| 113 | } |
| 114 | |
| 115 | protected function getDefaultSortOrder(): string { |
| 116 | return 'desc'; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Default group applied when the client does not send one. Useful when a |
| 121 | * listing's repository uses groups to gate "all" vs "trash" (or similar) |
| 122 | * and would otherwise return mixed results. |
| 123 | */ |
| 124 | protected function getDefaultGroup(): ?string { |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | protected function getDefaultPerPage(): int { |
| 129 | return self::DEFAULT_PER_PAGE; |
| 130 | } |
| 131 | |
| 132 | protected function getDefaultParameters(): array { |
| 133 | return []; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Subclasses may override to derive the listing definition's free-form |
| 138 | * `params` array from the request (e.g. a `?type=standard` query arg that |
| 139 | * routes to the same underlying repository). The default reuses |
| 140 | * {@see getDefaultParameters()} so callers without per-request params keep |
| 141 | * the existing behavior. |
| 142 | * |
| 143 | * @return array<string, mixed> |
| 144 | */ |
| 145 | protected function getRequestParameters(Request $request): array { |
| 146 | return $this->getDefaultParameters(); |
| 147 | } |
| 148 | |
| 149 | private function buildDefinition(Request $request): ListingDefinition { |
| 150 | $perPageParam = $request->getParam('per_page') ?? $request->getParam('limit'); |
| 151 | $perPage = is_numeric($perPageParam) |
| 152 | ? max(1, min(self::MAX_PER_PAGE, (int)$perPageParam)) |
| 153 | : $this->getDefaultPerPage(); |
| 154 | |
| 155 | $pageParam = $request->getParam('page'); |
| 156 | $offsetParam = $request->getParam('offset'); |
| 157 | $offset = is_numeric($pageParam) |
| 158 | ? (min(self::MAX_PAGE, max(1, (int)$pageParam)) - 1) * $perPage |
| 159 | : (is_numeric($offsetParam) ? (int)$offsetParam : 0); |
| 160 | |
| 161 | $orderByParam = $request->getParam('orderby') ?? $request->getParam('sort_by'); |
| 162 | $sortBy = is_string($orderByParam) && $orderByParam !== '' ? $orderByParam : $this->getDefaultSortBy(); |
| 163 | |
| 164 | $orderParam = $request->getParam('order') ?? $request->getParam('sort_order'); |
| 165 | $sortOrder = is_string($orderParam) && $orderParam !== '' ? strtolower($orderParam) : $this->getDefaultSortOrder(); |
| 166 | |
| 167 | $searchParam = $request->getParam('search'); |
| 168 | $search = is_string($searchParam) ? $searchParam : null; |
| 169 | |
| 170 | $groupParam = $request->getParam('group'); |
| 171 | $group = is_string($groupParam) && $groupParam !== '' ? $groupParam : $this->getDefaultGroup(); |
| 172 | |
| 173 | $filterParam = $request->getParam('filter'); |
| 174 | $filters = is_array($filterParam) ? $filterParam : []; |
| 175 | |
| 176 | return $this->listingHandler->getListingDefinition([ |
| 177 | 'offset' => $offset, |
| 178 | 'limit' => $perPage, |
| 179 | 'sort_by' => $sortBy, |
| 180 | 'sort_order' => $sortOrder, |
| 181 | 'search' => $search, |
| 182 | 'group' => $group, |
| 183 | 'filter' => $filters, |
| 184 | 'params' => $this->getRequestParameters($request), |
| 185 | ]); |
| 186 | } |
| 187 | } |
| 188 |