Appointments
2 weeks ago
Events
2 weeks ago
GetMobileInfoController.php
2 weeks ago
MobileV1Controller.php
2 weeks ago
MobileV1Controller.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Controller\Mobile; |
| 4 | |
| 5 | use AmeliaBooking\Application\Controller\Controller; |
| 6 | use AmeliaVendor\Psr\Http\Message\ServerRequestInterface as Request; |
| 7 | use AmeliaVendor\Psr\Http\Message\ResponseInterface as Response; |
| 8 | |
| 9 | /** |
| 10 | * Base controller for all /mobile/v1/ routes. |
| 11 | * |
| 12 | * Enforces two invariants so individual mobile controllers don't have to: |
| 13 | * |
| 14 | * 1. A Bearer token is required. If it is missing the response is a 409 JSON |
| 15 | * body with `data.reauthorize = true` — the same shape the mobile app |
| 16 | * already handles for expired sessions, so it drives the user back to |
| 17 | * the login screen rather than crashing. |
| 18 | * |
| 19 | * 2. The cabinet context (`source = cabinet-provider`) is forced by the route |
| 20 | * itself. Subclasses call `forceCabinetContext($command)` so the client |
| 21 | * never needs to send — or can fake — the source parameter. |
| 22 | */ |
| 23 | abstract class MobileV1Controller extends Controller |
| 24 | { |
| 25 | /** |
| 26 | * @param Request $request |
| 27 | * @param Response $response |
| 28 | * @param $args |
| 29 | * @param bool $validApiCall |
| 30 | * |
| 31 | * @return Response |
| 32 | */ |
| 33 | public function __invoke(Request $request, Response $response, $args, $validApiCall = false) |
| 34 | { |
| 35 | $authHeader = $request->getHeaderLine('Authorization'); |
| 36 | $parts = explode(' ', trim($authHeader)); |
| 37 | |
| 38 | if (count($parts) !== 2 || $parts[0] !== 'Bearer' || empty($parts[1])) { |
| 39 | $response = $response->withStatus(self::STATUS_CONFLICT); |
| 40 | $response = $response->withHeader('Content-Type', 'application/json;charset=utf-8'); |
| 41 | $response->getBody()->write( |
| 42 | json_encode(['message' => 'error', 'data' => ['reauthorize' => true]]) |
| 43 | ); |
| 44 | |
| 45 | return $response; |
| 46 | } |
| 47 | |
| 48 | return parent::__invoke($request, $response, $args, $validApiCall); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Forces cabinet-provider context on the command regardless of what the |
| 53 | * client sends. Subclasses call this inside instantiateCommand() instead |
| 54 | * of reading the `source` query param. |
| 55 | * |
| 56 | * @param \AmeliaBooking\Application\Commands\Command $command |
| 57 | */ |
| 58 | protected function forceCabinetContext($command) |
| 59 | { |
| 60 | $command->setPage('cabinet-provider'); |
| 61 | } |
| 62 | } |
| 63 |