HttpRequestCapabilities::sendDelete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Simplex\Quickstart\Shared\Testing;
4
5
use function GuzzleHttp\Psr7\stream_for;
6
use Psr\Container\ContainerInterface;
7
use Psr\Http;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Simplex\HttpApplication;
11
use Zend\Diactoros\Response;
12
use Zend\Diactoros\ServerRequestFactory;
13
use Zend\Diactoros\Uri;
14
15
trait HttpRequestCapabilities
16
{
17
    abstract public function getContainer(): ContainerInterface;
18
19
    public function sendGet(string $uri, array $queryParams = [], array $headers = []): ResponseInterface
20
    {
21
        $request = $this->createRequest('GET', $uri, $headers)
22
            ->withQueryParams($queryParams);
23
24
        return $this->send($request);
25
    }
26
27
    private function createRequest(string $method, string $uri, array $headers = []): ServerRequestInterface
28
    {
29
        $request = (ServerRequestFactory::fromGlobals([]))
30
            ->withMethod($method)
31
            ->withUri(new Uri($uri));
32
33
        if (empty($headers)) {
34
            return $request;
35
        }
36
37
        return $this->addHeaders($headers, $request);
38
    }
39
40
    private function addHeaders(array $headers, ServerRequestInterface $request): ServerRequestInterface
41
    {
42
        foreach ($headers as $headerName => $headerValue) {
43
            $request = $request->withHeader($headerName, $headerValue);
44
        }
45
46
        return $request;
47
    }
48
49
    private function send(ServerRequestInterface $request): ResponseInterface
50
    {
51
        $application = new HttpApplication($this->getContainer());
52
53
        return $application->handleRequest($request, new Response());
54
    }
55
56
    public function sendPost(string $uri, array $body, array $headers = []): ResponseInterface
57
    {
58
        $stream = stream_for(json_encode($body));
59
60
        $request = $this->createRequest('POST', $uri, $headers)
61
            ->withBody($stream);
62
63
        return $this->send($request);
64
    }
65
66
    public function sendPut(string $uri, array $body, array $headers = []): ResponseInterface
67
    {
68
        $stream = stream_for(json_encode($body));
69
70
        $request = $this->createRequest('PUT', $uri, $headers)
71
            ->withBody($stream);
72
73
        return $this->send($request);
74
    }
75
76
    public function sendDelete(string $uri, array $headers = []): ResponseInterface
77
    {
78
        $request = $this->createRequest('DELETE', $uri, $headers);
79
80
        return $this->send($request);
81
    }
82
83
    public function getBody(ResponseInterface $response): array
84
    {
85
        return json_decode((string) $response->getBody(), true);
86
    }
87
88
    public function getRawBody(ResponseInterface $response): string
89
    {
90
        return (string) $response->getBody();
91
    }
92
}
93