-
Notifications
You must be signed in to change notification settings - Fork 7
/
GrinService.php
executable file
·179 lines (151 loc) · 4.49 KB
/
GrinService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
declare(strict_types=1);
namespace Grin\Module\Model;
use Grin\Module\Api\GrinServiceInterface;
use Grin\Module\Model\Http\Client\Adapter\CurlFactory;
use Grin\Module\Model\Http\UriFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Serialize\Serializer\Json;
use Psr\Log\LoggerInterface;
class GrinService implements GrinServiceInterface
{
/**
* @var CurlFactory
*/
private $curlFactory;
/**
* @var SystemConfig
*/
private $systemConfig;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var UriFactory
*/
private $uriFactory;
/**
* @var Json
*/
private $json;
/**
* @var bool
*/
private $hasErrors = false;
/**
* @var int|null
*/
private $storeId;
/**
* @param CurlFactory $curlFactory
* @param SystemConfig $systemConfig
* @param LoggerInterface $logger
* @param UriFactory $uriFactory
* @param Json $json
*/
public function __construct(
CurlFactory $curlFactory,
SystemConfig $systemConfig,
LoggerInterface $logger,
UriFactory $uriFactory,
Json $json
) {
$this->curlFactory = $curlFactory;
$this->systemConfig = $systemConfig;
$this->logger = $logger;
$this->uriFactory = $uriFactory;
$this->json = $json;
}
/**
* @param string $topic
* @param array $data
* @return string|null
* @throws LocalizedException
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function send(string $topic, array $data): ?string
{
$this->storeId = (int) ($data['store_id'] ?? null);
unset($data['store_id']);
if (!$this->canSend()) {
return null;
}
$this->hasErrors = false;
$payload = $this->json->serialize($data);
$this->logger->info(sprintf('Sending the webhook "%s" %s', $topic, $payload));
try {
$curl = $this->curlFactory->create();
$curl->setCurlOption(CURLOPT_RETURNTRANSFER, true);
$curl->setCurlOption(CURLINFO_HEADER_OUT, true);
if ($curl instanceof \Zend_Http_Client_Adapter_Curl) {
$curl->setConfig(['timeout' => 30, 'maxredirects' => 1]);
}
$uri = $this->getUri();
$curl->connect($uri->getHost(), $uri->getPort(), true);
$curl->write('POST', $uri, 1.1, $this->getHeaders($payload, $topic), $payload);
[$header, $body] = explode("\r\n\r\n", $curl->read(), 2);
// phpcs:disable Magento2.Functions.DiscouragedFunction.Discouraged
$code = curl_getinfo($curl->getHandle(), CURLINFO_HTTP_CODE);
$curl->close();
if ($code !== 200) {
$this->hasErrors = true;
}
return $code . ' - ' . $body;
} catch (\Exception $e) {
$this->logger->critical($e->getMessage(), $e->getTrace());
throw new LocalizedException(__('Grin service webhook has failed'), $e);
}
}
/**
* @return bool
*/
public function hasErrors(): bool
{
return $this->hasErrors;
}
/**
* @return bool
*/
private function canSend(): bool
{
if (!$this->systemConfig->isGrinWebhookActive()) {
return false;
}
if (!$this->systemConfig->getWebhookToken($this->storeId)) {
$this->logger->critical('Authentication token has not been set up for Grin Webhooks');
return false;
}
return true;
}
/**
* @return \Laminas\Uri\Uri|\Zend_Uri_Http
* @throws \Zend_Uri_Exception
*/
private function getUri()
{
$uri = $this->uriFactory->create();
$url = $this->systemConfig->getGrinWebhookUrl();
if (!$uri instanceof \Laminas\Uri\Uri) {
$uri = \Zend_Uri_Http::fromString($url);
} else {
$uri->parse($url);
}
$uri->setPort($uri->getScheme() === 'https' ? 443 : 80);
return $uri;
}
/**
* @param string $payload
* @param string $topic
* @return string[]
*/
private function getHeaders(string $payload, string $topic): array
{
return [
'Content-Type: application/json',
'Content-Length: ' . strlen($payload),
'Authorization: ' . $this->systemConfig->getWebhookToken($this->storeId),
'Magento-Webhook-Topic: ' . $topic
];
}
}