<?php
namespace App\Service\Internal;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class ContactService
{
const SUPPORT_URL = '/v3/networks/%1/support';
/** @var string */
protected $networkId;
/** @var CoreApiService */
protected $coreApiService;
/** @var LoggerInterface */
protected $logger;
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var string
*/
protected $captchaPrivateKey;
public function __construct(
string $networkId,
CoreApiService $coreApiService,
LoggerInterface $logger,
RequestStack $rq,
string $captchaPrivateKey
) {
$this->networkId = $networkId;
$this->coreApiService = $coreApiService;
$this->logger = $logger;
$this->error = 0;
$this->requestStack = $rq;
$this->captchaPrivateKey = $captchaPrivateKey;
}
public function postContact($body)
{
$url = preg_replace(['/%1/'], [$this->networkId], $this->coreApiService->getApiCoreUrl() . self::SUPPORT_URL);
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'POST',
$url,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
['rawBody' => true],
json_decode($body)
);
return $response->status === 200;
}
public function getCategories(string $operator = '')
{
$url = preg_replace(['/%1/'], [$this->networkId], $this->coreApiService->getApiCoreUrl() . self::SUPPORT_URL);
$lang = strtoupper($this->requestStack->getCurrentRequest()->get('_locale'));
$params = ['language' => $lang];
if(!empty($operator)) {
$params['operator'] = $operator;
}
$response = $this->coreApiService->call(
CoreApiService::CALLER_TYPE_PUBLIC,
'GET',
$url,
$url,
['Accept' => 'application/json', 'Content-Type' => 'application/json'],
$params
);
if($response->status === 200 && isset($response->body->supportCategories)) {
return $response->body->supportCategories;
} else {
return [];
}
}
public function postCaptchaToGoogle($gresponse)
{
$url = 'https://www.google.com/recaptcha/api/siteverify';
$body = ['secret' => $this->captchaPrivateKey, 'response' => $gresponse];
$response = $this->coreApiService->callRest(
'POST',
$url,
[],
$body
);
return isset($response->body->success) ? $response->body->success : false;
}
}