| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace LaravelUi5\OData\Http; |
| 6: | |
| 7: | use Illuminate\Http\Request; |
| 8: | use LaravelUi5\OData\Exception\ForbiddenException; |
| 9: | use LaravelUi5\OData\Protocol\Execution\Engine; |
| 10: | use LaravelUi5\OData\Protocol\Planning\EntityQueryPlan; |
| 11: | use LaravelUi5\OData\Protocol\Planning\EntitySetQueryPlan; |
| 12: | use LaravelUi5\OData\Protocol\Planning\ExpandPruner; |
| 13: | use LaravelUi5\OData\Protocol\Planning\QueryPlan; |
| 14: | use LaravelUi5\OData\Service\Contracts\ReadAuthorizerInterface; |
| 15: | use LaravelUi5\OData\Service\Contracts\RuntimeSchemaInterface; |
| 16: | use LaravelUi5\OData\Service\ReadContext; |
| 17: | use LaravelUi5\OData\Service\ReadMessage; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | final readonly class ReadGate |
| 34: | { |
| 35: | public function __construct(private ReadAuthorizerInterface $authorizer) |
| 36: | { |
| 37: | } |
| 38: | |
| 39: | public function execute( |
| 40: | QueryPlan $plan, |
| 41: | Request $request, |
| 42: | RuntimeSchemaInterface $schema, |
| 43: | string $endpoint, |
| 44: | ): ODataResponse { |
| 45: | $read = new ReadContext(); |
| 46: | $this->authorizer->authorize($plan, $request, $read); |
| 47: | |
| 48: | if ($read->hasHardDenial()) { |
| 49: | throw ForbiddenException::fromContext($read); |
| 50: | } |
| 51: | |
| 52: | if ($read->dropped() !== []) { |
| 53: | $plan = $this->pruneDroppedExpands($plan, $read->dropped()); |
| 54: | } |
| 55: | |
| 56: | $response = (new Engine($schema, $endpoint))->execute($plan); |
| 57: | |
| 58: | if ($read->dropMessages() !== []) { |
| 59: | $response->headers->set('sap-messages', $this->encodeSapMessages($read->dropMessages())); |
| 60: | } |
| 61: | |
| 62: | return $response; |
| 63: | } |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | private function pruneDroppedExpands(QueryPlan $plan, array $droppedSetNames): QueryPlan |
| 73: | { |
| 74: | if ($plan instanceof EntitySetQueryPlan || $plan instanceof EntityQueryPlan) { |
| 75: | return $plan->withExpand(ExpandPruner::prune($plan->expand, $droppedSetNames)); |
| 76: | } |
| 77: | |
| 78: | return $plan; |
| 79: | } |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | private function encodeSapMessages(array $messages): string |
| 88: | { |
| 89: | return json_encode( |
| 90: | array_map(static fn (ReadMessage $message) => $message->toArray(), $messages), |
| 91: | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR, |
| 92: | ); |
| 93: | } |
| 94: | } |
| 95: | |