• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

phpolar / phpolar / 16086953532

05 Jul 2025 09:52AM UTC coverage: 95.833% (-4.2%) from 100.0%
16086953532

push

github

web-flow
feat: deprecate response builder (#451)

The ResponseBuilder and interface are superfluous.

Signed-off-by: Eric Fortmeyer <e.fortmeyer01@gmail.com>

9 of 10 new or added lines in 2 files covered. (90.0%)

8 existing lines in 2 files now uncovered.

207 of 216 relevant lines covered (95.83%)

5.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/src/Http/RequestProcessingHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Phpolar\Phpolar\Http;
6

7
use PhpCommonEnums\HttpResponseCode\Enumeration\HttpResponseCodeEnum as HttpResponseCode;
8
use Phpolar\HttpRequestProcessor\RequestProcessorExecutorInterface;
9
use Phpolar\ModelResolver\ModelResolverInterface;
10
use Phpolar\Phpolar\Http\Status\ClientError\BadRequest;
11
use Phpolar\Phpolar\Http\Status\ClientError\Forbidden;
12
use Phpolar\Phpolar\Http\Status\ClientError\NotFound;
13
use Phpolar\Phpolar\Http\Status\ClientError\Unauthorized;
14
use Phpolar\PropertyInjectorContract\PropertyInjectorInterface;
15
use Psr\Http\Message\ResponseFactoryInterface;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\StreamFactoryInterface;
19
use Psr\Http\Server\RequestHandlerInterface;
20

21
/**
22
 * Handles request routing for the application.
23
 */
24
final class RequestProcessingHandler implements RequestHandlerInterface
25
{
26
    public function __construct(
27
        private readonly ServerInterface $server,
28
        private readonly RequestProcessorExecutorInterface $processorExecutor,
29
        private readonly StreamFactoryInterface $streamFactory,
30
        private readonly ResponseFactoryInterface $responseFactory,
31
        private readonly AuthorizationCheckerInterface $authChecker,
32
        private readonly PropertyInjectorInterface $propertyInjector,
33
        private readonly ModelResolverInterface $modelResolver,
34
    ) {}
13✔
35

36
    /**
37
     * Attempts to locate and execute the target request processor.
38
     */
39
    public function handle(ServerRequestInterface $request): ResponseInterface
40
    {
41
        /**
42
         * Find the target for the request.
43
         *
44
         * Respond that the resource could not be found
45
         * or the method is not acceptable
46
         * or continue
47
         */
48
        $target = $this->server->findTarget($request);
13✔
49
        if ($target instanceof HttpResponseCode) {
13✔
50
            return $this->responseFactory->createResponse(
2✔
51
                (int) $target->value,
2✔
52
                $target->getLabel()
2✔
53
            );
2✔
54
        }
55

56
        /**
57
         * Determine what representation will be used.
58
         * Respond that the request is not acceptable
59
         * or continue
60
         */
61
        $responseCode = $target->negotiate($request);
11✔
62
        if ($responseCode === HttpResponseCode::NotAcceptable) {
11✔
63
            return $this->responseFactory
1✔
64
                ->createResponse((int) $responseCode->value, $responseCode->getLabel());
1✔
65
        }
66

67
        $requestProcessor = $target->requestProcessor;
10✔
68

69
        /**
70
         * Use "not authorized" response if the request is not authorized
71
         * or continue
72
         */
73
        $authCheckResult = $this->authChecker->authorize($requestProcessor, $request);
10✔
74
        if ($authCheckResult instanceof ResponseInterface) {
10✔
75
            return $authCheckResult;
1✔
76
        }
77

78
        /**
79
         * Get path variables
80
         */
81
        $pathVariables = new PathVariableBindings(
9✔
82
            $target->location,
9✔
83
            $request->getUri()->getPath()
9✔
84
        );
9✔
85

86
        /**
87
         * Get model from parsed body
88
         */
89
        $models = $this->modelResolver->resolve(
9✔
90
            $requestProcessor,
9✔
91
            "process",
9✔
92
        );
9✔
93

94
        /**
95
         * Merge path variables and models
96
         */
97
        $args = array_merge(
9✔
98
            $models,
9✔
99
            $pathVariables->toArray(),
9✔
100
        );
9✔
101

102
        /**
103
         * Handle property dependency injection
104
         */
105
        $this->propertyInjector->inject($requestProcessor);
9✔
106

107
        $resource = $this->processorExecutor->execute(
9✔
108
            $requestProcessor,
9✔
109
            $args,
9✔
110
        );
9✔
111

112
        if ($resource instanceof NotFound) {
9✔
UNCOV
113
            $responseCode = HttpResponseCode::NotFound;
×
114
        }
115

116
        if ($resource instanceof Unauthorized) {
9✔
UNCOV
117
            $responseCode = HttpResponseCode::Unauthorized;
×
118
        }
119

120
        if ($resource instanceof Forbidden) {
9✔
UNCOV
121
            $responseCode = HttpResponseCode::Forbidden;
×
122
        }
123

124
        if ($resource instanceof BadRequest) {
9✔
125
            $responseCode = HttpResponseCode::BadRequest;
4✔
126
        }
127

128
        $representation = $target->getRepresentation($resource);
9✔
129

130
        return $this->responseFactory
9✔
131
            ->createResponse(
9✔
132
                code: (int) $responseCode->value,
9✔
133
                reasonPhrase: $responseCode->getLabel()
9✔
134
            )
9✔
135
            ->withBody($this->streamFactory->createStream((string) $representation))
9✔
136
            ->withHeader("Content-Type", $representation->getMimeType());
9✔
137
    }
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc