BaseIMCloud::httpPostJson()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 3
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/14/2018
6
 * Time: 1:56 PM
7
 */
8
9
namespace TimSDK\Core;
10
11
use TimSDK\Support\Json;
12
use TimSDK\Foundation\ResponseBag;
13
use TimSDK\Container\ServiceContainer;
14
use Psr\Http\Message\ResponseInterface;
15
16
class BaseIMCloud
17
{
18
    /**
19
     * @var ServiceContainer
20
     */
21
    protected $app;
22
23
    /**
24
     * AbstractTimSDKAPI constructor.
25
     * @param ServiceContainer $app
26
     */
27 6
    public function __construct(ServiceContainer $app)
28
    {
29 6
        $this->app = $app;
30
31 6
        $this->initialize();
32 2
    }
33
34
    /**
35
     * Init
36
     */
37
    public function initialize()
38
    {
39
    }
40
41
    /**
42
     * POST request.
43
     *
44
     * @param       $uri
45
     * @param array $data
46
     * @param array $options
47
     * @return ResponseBag
48
     */
49
    public function httpPost($uri, $data = [], $options = [])
50
    {
51
        return $this->request($uri, 'POST', array_merge($options, [
52
            'form_params' => $data
53
        ]));
54
    }
55
56
    /**
57
     * JSON request.
58
     *
59
     * @param       $uri
60
     * @param array $data
61
     * @param array $options
62
     * @return ResponseBag
63
     * @throws Exceptions\JsonParseException
64
     */
65
    public function httpPostJson($uri, $data = [], $options = [])
66
    {
67
        return $this->request($uri, 'POST', array_merge($options, [
68
            'body' => is_array($data) ? Json::encode($data) : $data
69
        ]));
70
    }
71
72
    /**
73
     * @param string $method
74
     * @param string $uri
75
     * @param array  $options
76
     * @return ResponseBag
77
     */
78
    public function request($uri, $method = 'GET', $options = [])
79
    {
80
        /**
81
         * @var ResponseInterface $response
82
         */
83
        $response = $this->app->httpClient->request($method, $uri, $options);
84
85
        return new ResponseBag(
86
            $response->getBody()->getContents(),
87
            $response->getHeaders(),
88
            $response->getStatusCode()
89
        );
90
    }
91
}
92