Skip to content

Commit bbaae13

Browse files
committed
Create HttpFactory for creating Response objects
1 parent 80a4959 commit bbaae13

File tree

4 files changed

+55
-95
lines changed

4 files changed

+55
-95
lines changed

src/Redmine/Api/AbstractApi.php

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Redmine\Exception;
1010
use Redmine\Exception\SerializerException;
1111
use Redmine\Http\HttpClient;
12+
use Redmine\Http\HttpFactory;
1213
use Redmine\Http\Request;
1314
use Redmine\Http\Response;
1415
use Redmine\Serializer\JsonSerializer;
@@ -73,7 +74,7 @@ final protected function getHttpClient(): HttpClient
7374

7475
final protected function getLastResponse(): Response
7576
{
76-
return $this->lastResponse !== null ? $this->lastResponse : $this->createResponse(0, '', '');
77+
return $this->lastResponse !== null ? $this->lastResponse : HttpFactory::makeResponse(0, '', '');
7778
}
7879

7980
/**
@@ -412,16 +413,12 @@ private function getResponseAsArray(Response $response): array
412413

413414
private function handleClient(Client $client): HttpClient
414415
{
415-
$responseFactory = Closure::fromCallable([$this, 'createResponse']);
416-
417-
return new class ($client, $responseFactory) implements HttpClient {
416+
return new class ($client) implements HttpClient {
418417
private $client;
419-
private $responseFactory;
420418

421-
public function __construct(Client $client, Closure $responseFactory)
419+
public function __construct(Client $client)
422420
{
423421
$this->client = $client;
424-
$this->responseFactory = $responseFactory;
425422
}
426423

427424
public function request(Request $request): Response
@@ -436,7 +433,7 @@ public function request(Request $request): Response
436433
$this->client->requestGet($request->getPath());
437434
}
438435

439-
return ($this->responseFactory)(
436+
return HttpFactory::makeResponse(
440437
$this->client->getLastResponseStatusCode(),
441438
$this->client->getLastResponseContentType(),
442439
$this->client->getLastResponseBody()
@@ -445,37 +442,6 @@ public function request(Request $request): Response
445442
};
446443
}
447444

448-
private function createResponse(int $statusCode, string $contentType, string $body): Response
449-
{
450-
return new class ($statusCode, $contentType, $body) implements Response {
451-
private $statusCode;
452-
private $contentType;
453-
private $body;
454-
455-
public function __construct(int $statusCode, string $contentType, string $body)
456-
{
457-
$this->statusCode = $statusCode;
458-
$this->contentType = $contentType;
459-
$this->body = $body;
460-
}
461-
462-
public function getStatusCode(): int
463-
{
464-
return $this->statusCode;
465-
}
466-
467-
public function getContentType(): string
468-
{
469-
return $this->contentType;
470-
}
471-
472-
public function getContent(): string
473-
{
474-
return $this->body;
475-
}
476-
};
477-
}
478-
479445
private function createRequest(string $method, string $path, string $contentType, string $content = ''): Request
480446
{
481447
return new class ($method, $path, $contentType, $content) implements Request {

src/Redmine/Client/NativeCurlClient.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Redmine\Exception\ClientException;
88
use Redmine\Http\HttpClient;
9+
use Redmine\Http\HttpFactory;
910
use Redmine\Http\Request;
1011
use Redmine\Http\Response;
1112

@@ -72,37 +73,11 @@ public function request(Request $request): Response
7273
$request->getContentType()
7374
);
7475

75-
return new class (
76+
return HttpFactory::makeResponse(
7677
$this->lastResponseStatusCode,
7778
$this->lastResponseContentType,
7879
$this->lastResponseBody
79-
) implements Response {
80-
private $statusCode;
81-
private $contentType;
82-
private $body;
83-
84-
public function __construct(int $statusCode, string $contentType, string $body)
85-
{
86-
$this->statusCode = $statusCode;
87-
$this->contentType = $contentType;
88-
$this->body = $body;
89-
}
90-
91-
public function getStatusCode(): int
92-
{
93-
return $this->statusCode;
94-
}
95-
96-
public function getContentType(): string
97-
{
98-
return $this->contentType;
99-
}
100-
101-
public function getContent(): string
102-
{
103-
return $this->body;
104-
}
105-
};
80+
);
10681
}
10782

10883
/**

src/Redmine/Client/Psr18Client.php

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Http\Message\StreamFactoryInterface;
1515
use Redmine\Exception\ClientException;
1616
use Redmine\Http\HttpClient;
17+
use Redmine\Http\HttpFactory;
1718
use Redmine\Http\Request;
1819
use Redmine\Http\Response;
1920

@@ -90,37 +91,11 @@ public function request(Request $request): Response
9091
$request->getContentType()
9192
);
9293

93-
return new class (
94+
return HttpFactory::makeResponse(
9495
$response->getStatusCode(),
9596
$response->getHeaderLine('Content-Type'),
9697
strval($response->getBody())
97-
) implements Response {
98-
private $statusCode;
99-
private $contentType;
100-
private $body;
101-
102-
public function __construct(int $statusCode, string $contentType, string $body)
103-
{
104-
$this->statusCode = $statusCode;
105-
$this->contentType = $contentType;
106-
$this->body = $body;
107-
}
108-
109-
public function getStatusCode(): int
110-
{
111-
return $this->statusCode;
112-
}
113-
114-
public function getContentType(): string
115-
{
116-
return $this->contentType;
117-
}
118-
119-
public function getContent(): string
120-
{
121-
return $this->body;
122-
}
123-
};
98+
);
12499
}
125100

126101
/**

src/Redmine/Http/HttpFactory.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Redmine\Http;
6+
7+
/**
8+
* Factory for HTTP objects.
9+
*
10+
* @internal
11+
*/
12+
final class HttpFactory
13+
{
14+
public static function makeResponse(int $statusCode, string $contentType, string $content): Response
15+
{
16+
return new class ($statusCode, $contentType, $content) implements Response {
17+
private $statusCode;
18+
private $contentType;
19+
private $body;
20+
21+
public function __construct(int $statusCode, string $contentType, string $body)
22+
{
23+
$this->statusCode = $statusCode;
24+
$this->contentType = $contentType;
25+
$this->body = $body;
26+
}
27+
28+
public function getStatusCode(): int
29+
{
30+
return $this->statusCode;
31+
}
32+
33+
public function getContentType(): string
34+
{
35+
return $this->contentType;
36+
}
37+
38+
public function getContent(): string
39+
{
40+
return $this->body;
41+
}
42+
};
43+
}
44+
}

0 commit comments

Comments
 (0)