module = $module; $this->jwt = $psAccountsAdapterService->getOrRefreshToken(); $this->shopId = $psAccountsAdapterService->getShopUuid(); $this->liveSyncApiUrl = $liveSyncApiUrl; } /** * @see https://docs.guzzlephp.org/en/stable/quickstart.html- * * @param int $timeout * * @return HttpClientInterface */ private function getClient($timeout = Config::SYNC_API_MAX_TIMEOUT) { return (new ClientFactory())->getClient([ 'allow_redirects' => true, 'connect_timeout' => 5, 'http_errors' => false, 'timeout' => $timeout, ]); } /** * @param string $shopContent * @param int $shopContentId * @param string $action * * @return array */ public function liveSync($shopContent, $shopContentId, $action) { // shop content send to the API must be in kebab-case $kebabCasedShopContent = str_replace('_', '-', $shopContent); $response = $this->getClient(3)->sendRequest( new Request( 'POST', $this->liveSyncApiUrl . '/notify/' . $this->shopId, [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->jwt, 'User-Agent' => 'ps-eventbus/' . $this->module->version, 'Content-Type' => 'application/json', ], '{"shopContents": ["' . $kebabCasedShopContent . '"], "shopContentId": ' . $shopContentId . ', "action": "' . $action . '"}' ) ); return [ 'status' => substr((string) $response->getStatusCode(), 0, 1) === '2', 'httpCode' => $response->getStatusCode(), 'body' => $response->getBody(), ]; } }