Test Failed
Push — master ( 90f1e5...8cd561 )
by Jim
02:25
created

BaseIMCloud::httpPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 3
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 GuzzleHttp\Client;
12
use TimSDK\Support\Json;
13
use TimSDK\Foundation\Application;
14
use TimSDK\Foundation\ResponseBag;
15
use TimSDK\Container\ServiceContainer;
16
use Psr\Http\Message\ResponseInterface;
17
18
class BaseIMCloud
19
{
20
    /**
21
     * @var Application
22
     */
23
    protected $app;
24
25
    /**
26
     * AbstractTimSDKAPI constructor.
27
     * @param ServiceContainer $app
28
     */
29
    public function __construct(ServiceContainer $app)
30
    {
31
        $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
$app is of type TimSDK\Container\ServiceContainer, but the property $app was declared to be of type TimSDK\Foundation\Application. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
32
33
        $this->initialize();
34
    }
35
36
    /**
37
     * Init
38
     */
39
    public function initialize()
40
    {
41
    }
42
43
    /**
44
     * POST request.
45
     *
46
     * @param       $uri
47
     * @param array $data
48
     * @param array $options
49
     * @return ResponseBag
50
     */
51
    public function httpPost($uri, $data = [], $options = [])
52
    {
53
        return $this->request($uri, 'POST', array_merge($options, [
54
            'form_params' => $data
55
        ]));
56
    }
57
58
    /**
59
     * JSON request.
60
     *
61
     * @param       $uri
62
     * @param array $data
63
     * @param array $options
64
     * @return ResponseBag
65
     * @throws Exceptions\JsonParseException
66
     */
67
    public function httpPostJson($uri, $data = [], $options = [])
68
    {
69
        return $this->request($uri, 'POST', array_merge($options, [
70
            'body' => is_array($data) ? Json::encode($data) : $data
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
71
        ]));
72
    }
73
74
    /**
75
     * @param string $method
76
     * @param string $uri
77
     * @param array  $options
78
     * @return ResponseBag
79
     */
80
    public function request($uri, $method = 'GET', $options = [])
81
    {
82
        /**
83
         * @var ResponseInterface $response
84
         */
85
        $response = $this->app->httpClient->request($method, $uri, $options);
86
87
        return new ResponseBag(
88
            $response->getBody()->getContents(),
89
            $response->getHeaders(),
90
            $response->getStatusCode()
91
        );
92
    }
93
}
94