Test Failed
Push — main ( f68dd4...f46f5c )
by Bingo
14:33
created

ConnectUtil::assembleRequestParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 16
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
namespace Jabe\Engine\Impl\Util;
4
5
class ConnectUtil
6
{
7
    // request
8
    public const PARAM_NAME_REQUEST_URL = "url";
9
    public const PARAM_NAME_REQUEST_METHOD = "method";
10
    public const PARAM_NAME_REQUEST_PAYLOAD = "payload";
11
    public const PARAM_NAME_REQUEST_CONFIG = "request-config";
12
13
    // request methods
14
    public const METHOD_NAME_POST = "POST";
15
16
    // config options
17
    public const CONFIG_NAME_CONNECTION_TIMEOUT = "connection-timeout";
18
    public const CONFIG_NAME_SOCKET_TIMEOUT = "socket-timeout";
19
20
    // response
21
    public const PARAM_NAME_RESPONSE_STATUS_CODE = "statusCode";
22
    public const PARAM_NAME_RESPONSE = "response";
23
24
    // common between request and response
25
    public const PARAM_NAME_HEADERS = "headers";
26
    public const HEADER_CONTENT_TYPE = "Content-Type";
27
28
    // helper methods
29
    public static function assembleRequestParameters(
30
        string $methodName,
31
        string $url,
32
        string $contentType,
33
        string $payload
34
    ): array {
35
        $requestHeaders = [];
36
        $requestHeaders[self::HEADER_CONTENT_TYPE] = $contentType;
37
38
        $requestParams = [];
39
        $requestParams[self::PARAM_NAME_REQUEST_METHOD] = $methodName;
40
        $requestParams[self::PARAM_NAME_REQUEST_URL] = $url;
41
        $requestParams[self::PARAM_NAME_HEADERS] = $requestHeaders;
42
        $requestParams[self::PARAM_NAME_REQUEST_PAYLOAD] = $payload;
43
44
        return $requestParams;
45
    }
46
47
    public static function addRequestTimeoutConfiguration(array $requestParams, int $timeout): array
48
    {
49
        $config = [];
50
        $config[self::CONFIG_NAME_CONNECTION_TIMEOUT] = $timeout;
51
        $config[self::CONFIG_NAME_SOCKET_TIMEOUT] = $timeout;
52
53
        $requestParams[self::PARAM_NAME_REQUEST_CONFIG] = $config;
54
55
        return $requestParams;
56
    }
57
}
58