Request::send()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace AsimovExpress\AdobeConnect\Http;
4
5
use XMLWriter;
6
use Psr\Log\LoggerAwareInterface;
7
use Psr\Log\LoggerAwareTrait;
8
9
use AsimovExpress\AdobeConnect\Http\InvalidMethodException;
10
11
class Request implements LoggerAwareInterface
12
{
13
    use LoggerAwareTrait;
14
15
    /**
16
     * Parameters to be send in the request.
17
     *
18
     * @var array
19
     */
20
    protected $parameters;
21
22
    /**
23
     * Headers to be send most likely to keep unchanged.
24
     *
25
     * @var array
26
     */
27
    protected $headers;
28
29
    /**
30
     * Adobe Connect API path
31
     *
32
     * @var string
33
     */
34
    protected $apiPath;
35
36
    /**
37
     * HTTP method to perform the request to the Adobe Connect API.
38
     * Adobe Connect only supports 'POST' and 'GET' methods.
39
     *
40
     * @var string
41
     */
42
    protected $method = 'POST';
43
44
    /**
45
     * Request constructor
46
     *
47
     * @param string $apiPath Path of the Adobe Connect API.
48
     * @param array $parameters Parameters to be send to the API.
49
     * @param array $headers Custom headers to be send to the API.
50
     */
51
    public function __construct($apiPath, $parameters = [], $headers = [])
52
    {
53
        $this->apiPath = $apiPath;
54
        $this->parameters = $parameters;
55
        $this->headers = array_merge([
56
            'content-type' => 'application/xml'
57
        ], $headers);
58
    }
59
60
    /**
61
     * Adds a list of params to the request.
62
     *
63
     * @param array $parameters Hash table with parameters to be added.
64
     *
65
     * @return void
66
     */
67
    public function addParameters($parameters)
68
    {
69
        $this->parameters = array_merge($this->parameters, $parameters);
70
    }
71
72
    /**
73
     * Adds a single parameter.
74
     *
75
     * @return void
76
     */
77
    public function addParameter($parameter, $value)
78
    {
79
        $this->addParameters([$parameter => $value]);
80
    }
81
82
    /**
83
     * Return the request body to be sent to the Adobe Connect API.
84
     *
85
     * @return string
86
     */
87
    public function getXMLBody()
88
    {
89
        $writer = new XMLWriter();
90
        $writer->openMemory();
91
        $writer->startElement('params');
92
        foreach ($this->parameters as $parameter => $value) {
93
            $writer->startelement('param');
94
            $writer->startAttribute('name');
95
            $writer->text($parameter);
96
            $writer->endAttribute();
97
            $writer->text($value);
98
            $writer->endElement();
99
        }
100
        $writer->endElement();
101
        $xml = $writer->outputMemory();
102
        $writer->flush();
103
        return $xml;
104
    }
105
106
    /**
107
     * Returns all headers set for the request.
108
     *
109
     * @return array
110
     */
111
    public function getHeaders()
112
    {
113
        return $this->headers;
114
    }
115
116
    /**
117
     * Sets a different HTTP method.
118
     *
119
     * @param string $method The http method to be used in API calls.
120
     */
121
    public function setMethod($method)
122
    {
123
        $this->method = $method;
124
    }
125
126
    /**
127
     * Calls the remote Adobe Connect API.
128
     *
129
     * @return Response
130
     */
131
    public function send()
132
    {
133
        if ($this->method === 'POST') {
134
            return $this->post();
135
        }
136
137
        throw new InvalidMethodException($this->method);
138
    }
139
140
    /**
141
     * Calls the remote Adobe Connect API using HTTP POST method.
142
     *
143
     * @return Response
144
     */
145
    public function post()
146
    {
147
        $resource = curl_init();
148
        $xml = $this->getXMLBody();
149
150
        curl_setopt($resource, CURLOPT_URL, $this->apiPath);
151
        curl_setopt($resource, CURLOPT_POST, true);
152
        foreach ($this->headers as $header => $value) {
153
            curl_setopt($resource, CURLOPT_HTTPHEADER, [$header, $value]);
154
        }
155
        curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
156
        curl_setopt($resource, CURLOPT_POSTFIELDS, $xml);
157
        $result = curl_exec($resource);
158
159
        if ($this->logger) {
160
            $this->logger->info('AdobeConnect request using POST method', [
161
                'url' => $this->apiPath,
162
                'headers' => $this->headers,
163
                'post_body' => $xml
164
            ]);
165
        }
166
167
        return new Response($resource, $result);
168
    }
169
}
170