RequestHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DevoraliveCachet\Http;
4
5
use DevoraliveCachet\Client as Client;
6
use \GuzzleHttp\Client as GuzzleClient;
7
use DevoraliveCachet\Entity\Entity;
8
use DevoraliveCachet\Result\Envelope;
9
use GuzzleHttp\Exception\GuzzleException;
10
use JMS\Serializer\SerializerInterface;
11
use Psr\Http\Message\ResponseInterface;
12
13
/**
14
 * Class RequestHandler
15
 * @package DevoraliveCachet\Http
16
 */
17
class RequestHandler
18
{
19
    /**
20
     * @var GuzzleClient
21
     */
22
    protected $client;
23
24
    /**
25
     * @var array
26
     */
27
    private $headers;
28
29
    /**
30
     * @var
31
     */
32
    protected $serializer;
33
34
    /**
35
     * RequestHandler constructor.
36
     *
37
     * @param Client     $client
38
     * @param SerializerInterface $serializer
39
     */
40
    public function __construct(Client $client, SerializerInterface $serializer)
41
    {
42
        $this->client = new GuzzleClient([
43
            'base_uri' => $client->getEndpoint()
44
        ]);
45
46
        $this->headers = [
47
            'Content-type' => 'application/json',
48
            'X-Cachet-Token' => $client->getToken()
49
        ];
50
51
        $this->serializer = $serializer;
52
    }
53
54
    /**
55
     * @param ResponseInterface $response
56
     * @param $envelopeType
57
     *
58
     * @return Envelope
59
     */
60
    private function parseResponse(ResponseInterface $response, $envelopeType)
61
    {
62
        $json = $response->getBody();
63
        $envelope = $this->serializer->deserialize($json, $envelopeType, 'json');
64
65
        return $envelope;
66
    }
67
68
    /**
69
     * @param       $method
70
     * @param       $url
71
     * @param array $params
72
     *
73
     * @return ResponseInterface
74
     * @throws GuzzleException
75
     */
76
    public function request($method, $url, array $params = [])
77
    {
78
        $params = array_merge($params, [
79
            'headers' => $this->headers
80
        ]);
81
        return $this->client->request($method, $url, $params);
82
    }
83
84
    /**
85
     * @param       $path
86
     * @param       $envelopeType
87
     * @param array $params
88
     *
89
     * @return Envelope
90
     * @throws GuzzleException
91
     */
92
    public function get($path, $envelopeType, array $params = [])
93
    {
94
        $response = $this->request("GET", $path, [
95
            'query' => $params
96
        ]);
97
        $envelope = $this->parseResponse($response, $envelopeType);
98
99
        return $envelope;
100
    }
101
102
    /**
103
     * @param        $path
104
     * @param        $envelopeType
105
     * @param Entity $entity
106
     *
107
     * @return Envelope
108
     * @throws GuzzleException
109
     */
110
    public function put($path, $envelopeType, Entity $entity)
111
    {
112
        $data = $this->serializer->serialize($entity, 'json');
113
        $response = $this->request("PUT", $path, [
114
            'body' => $data
115
        ]);
116
117
        $envelope = $this->parseResponse($response, $envelopeType);
118
119
        return $envelope;
120
    }
121
122
    /**
123
     * @param        $path
124
     * @param        $envelopeType
125
     * @param Entity $entity
126
     *
127
     * @return Envelope
128
     * @throws GuzzleException
129
     */
130
    public function post($path, $envelopeType, Entity $entity)
131
    {
132
        $data = $this->serializer->serialize($entity, 'json');
133
        $response = $this->request("POST", $path, [
134
            'body' => $data
135
        ]);
136
137
        $envelope = $this->parseResponse($response, $envelopeType);
138
139
        return $envelope;
140
    }
141
142
    /**
143
     * @param $path
144
     * @param $id
145
     *
146
     * @throws GuzzleException
147
     */
148
    public function delete($path, $id)
149
    {
150
        $this->request("DELETE", $path, [
151
            'json' => [
152
                'id' => $id
153
            ]
154
        ]);
155
    }
156
}
157