-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMageClient.php
125 lines (103 loc) · 3.45 KB
/
MageClient.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
<?php
namespace Sailthru\MageSail;
use Magento\Framework\DataObject;
use Magento\Framework\Module\ModuleListInterface;
use Magento\Store\Model\StoreManager;
use Sailthru\MageSail\Helper\ScopeResolver;
class MageClient extends \Sailthru_Client
{
public $_eventType = null;
/** @var string */
private $version;
/** @var Logger */
private $logger;
/** @var ScopeResolver */
private $scopeResolver;
protected static $TIMEOUT_OPTIONS = [
"timeout" => 3000,
"connection_timeout" => 3000
];
protected static $TRUNCATE_RESPONSES = [
["GET", "template"],
["GET", "settings"]
];
public function __construct($api_key, $secret, $version, Logger $logger, ScopeResolver $scopeResolver)
{
parent::__construct($api_key, $secret, false, self::$TIMEOUT_OPTIONS);
$this->logger = $logger;
$this->scopeResolver = $scopeResolver;
$this->version = $version;
}
protected function httpRequest($action, $data, $method = 'POST', $options = [])
{
$logAction = "{$method} /{$action}";
$scope= $this->scopeResolver->getScope();
$scopeString = "{$scope[0]} {$scope[1]}";
$this->logger->info(var_export([
'action' => $logAction,
'event_type' => $this->_eventType,
'scope' => $scopeString,
'http_request_type' => $this->http_request_type,
'body' => $data['json']
], true));
try {
$response = parent::httpRequest($action, $data, $method, $options);
$this->logger->info(var_export([
'action_response' => $logAction,
'event_type ' => $this->_eventType,
'scope' => $scopeString,
'response' => !$this->truncateResponse($method, $action) ? $response : "<TRUNCATED>"
], true));
return $response;
} catch (\Sailthru_Client_Exception $e) {
$this->logger->error(var_export([
'error' => $logAction,
'scope' => $scopeString,
'code' => $e->getCode(),
'message' => $e->getMessage(),
], true));
throw $e;
}
}
protected function prepareJsonPayload(array $data, array $binary_data = [])
{
$data['integration'] = "Magento 2 - $this->version";
return parent::prepareJsonPayload($data, $binary_data);
}
/**
* @return array
*/
public function getSettings()
{
return $this->apiGet('settings');
}
/**
* @return array
*/
public function getVerifiedSenders()
{
$settings = $this->getSettings();
return $settings['from_emails'] ?: [];
}
/**
* Log messages. Moved to its own class.
* @param $message
* @deprecated
*/
public function logger($message)
{
if ($message instanceof \Throwable) {
$this->logger->info($message->getMessage(), $message->getTrace());
return;
}
if ($message instanceof DataObject) {
$this->logger->info(var_export($message->debug(), true));
return;
}
$this->logger->info(!is_string($message) ? var_export($message, true) : $message);
}
private function truncateResponse($method, $action)
{
return in_array([$method, $action], self::$TRUNCATE_RESPONSES);
}
}