Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
71.43% covered (warning)
71.43%
5 / 7
CRAP
94.74% covered (success)
94.74%
36 / 38
Request
0.00% covered (danger)
0.00%
0 / 1
71.43% covered (warning)
71.43%
5 / 7
9.01
94.74% covered (success)
94.74%
36 / 38
 __construct
0.00% covered (danger)
0.00%
0 / 1
1.00
91.67% covered (success)
91.67%
11 / 12
 __get
0.00% covered (danger)
0.00%
0 / 1
2.15
66.67% covered (warning)
66.67%
2 / 3
 makeRequest
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
13 / 13
 setEndpoint
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 call
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 addHeaders
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 addData
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
1<?php
2
3namespace LDC\LDCCRM\Transport;
4
5use GuzzleHttp\Client;
6use GuzzleHttp\Psr7\Request as Psr7Request;
7use GuzzleHttp\Psr7\Utils as Psr7Utils;
8use GuzzleHttp\RequestOptions;
9use GuzzleHttp\Utils as GuzzleHttpUtils;
10use LDC\LDCCRM\Enums\HttpMethods;
11use LDC\LDCCRM\Utils;
12use Psr\Http\Message\ResponseInterface;
13
14/**
15 * The LDC CRM request
16 * Contains the http method, the requested url and data to pass
17 *
18 * @property \LDC\LDCCRM\Enums\HttpMethods $method readonly method
19 * @property string $uri readonly uri
20 * @property array $headers headers
21 * @property array $data readonly data
22 *
23 * @package LDCCrmClient
24 * @subpackage transport
25 */
26class Request
27{
28    /**
29     * GuzzleHttp client
30     *
31     * @var \GuzzleHttp\Client
32     */
33    private $client;
34
35    /**
36     * The http method
37     *
38     * @var \LDC\LDCCRM\Enums\HttpMethods
39     */
40    protected $method;
41
42    /**
43     * The http url
44     *
45     * @var string
46     */
47    protected $uri;
48
49    /**
50     * Data to pass to the http headers
51     *
52     * @var array
53     */
54    protected $headers = [];
55
56    /**
57     * Data to pass to the http body
58     *
59     * @var array
60     */
61    protected $data = [];
62
63    /**
64     * Guzzle request
65     *
66     * @var callable the returns \GuzzleHttp\Psr7\Request
67     */
68    private $request;
69
70    /**
71     * The Api endpoint
72     *
73     * @var string
74     */
75    private $endpoint;
76
77    /**
78     * Request
79     *
80     * @param \LDC\LDCCRM\Enums\HttpMethods $method
81     * @param string                        $uri
82     * @param array                         $headers
83     * @param array                         $data
84     */
85    public function __construct(
86        HttpMethods $method,
87        string $uri,
88        array $headers = [],
89        array $data = []
90    ) {
91        $this->hidden  = function () {
92            return [];
93        };
94        $this->method  = $method;
95        $this->uri     = $uri;
96        $this->data    = $data;
97        $this->headers = $headers;
98        $this->client  = new Client([
99            RequestOptions::HEADERS => [
100                'User-Agent' => sprintf(
101                    'LDC CRM Client v%s',
102                    Utils::getPackageInfo()['version']
103                )
104            ]
105        ]);
106    }
107
108    /**
109     * Generic accessor
110     *
111     * @param string $field
112     * @return mixed|null
113     */
114    public function __get(string $field)
115    {
116        if (\in_array($field, ['method', 'uri', 'data'])) {
117            return $this->{$field};
118        }
119        return null;
120    }
121
122    /**
123     * make the guzzle request
124     *
125     * @return void
126     */
127    private function makeRequest(): void
128    {
129        $this->request = function () {
130            $uri = $this->uri;
131            if ($this->method->equals(HttpMethods::get())) {
132                $uri        = sprintf("%s?%s", $uri, http_build_query($this->data));
133                $this->data = [];
134            }
135            $request = new Psr7Request(
136                \strtoupper($this->method),
137                sprintf('%s/%s', $this->endpoint, $uri),
138                $this->headers
139            );
140            $request = Psr7Utils::modifyRequest($request, [
141                'body' => GuzzleHttpUtils::jsonEncode($this->data)
142            ]);
143            return $request;
144        };
145    }
146
147    /**
148     * Set the api endpoint to the request
149     *
150     * @param string $endpoint
151     * @return void
152     */
153    public function setEndpoint(string $endpoint): void
154    {
155        $this->endpoint = $endpoint;
156    }
157
158    /**
159     * Make the Http request
160     *
161     * @return ResponseInterface
162     */
163    public function call(): ResponseInterface
164    {
165        $this->makeRequest();
166        /** @var \GuzzleHttp\Psr7\Request */
167        $request = ($this->request)();
168        return $this->client->send($request, [
169            // ! Do not throw HTTP Exceptions.
170            'http_errors' => false,
171            'allow_redirects' => false
172        ]);
173    }
174
175    /**
176     * Add headers to the request
177     *
178     * @param array $headers
179     * @return void
180     */
181    public function addHeaders(array $headers)
182    {
183        $this->headers = \array_merge($this->headers, $headers);
184    }
185
186    /**
187     * Add data to the request
188     *
189     * @param array $data
190     * @return void
191     */
192    public function addData(array $data)
193    {
194        $this->data = \array_merge($this->data, $data);
195    }
196}