src/Service/Internal/ContactService.php line 63

Open in your IDE?
  1. <?php
  2. namespace App\Service\Internal;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. class ContactService
  6. {
  7. const SUPPORT_URL = '/v3/networks/%1/support';
  8. /** @var string */
  9. protected $networkId;
  10. /** @var CoreApiService */
  11. protected $coreApiService;
  12. /** @var LoggerInterface */
  13. protected $logger;
  14. /**
  15. * @var RequestStack
  16. */
  17. protected $requestStack;
  18. /**
  19. * @var string
  20. */
  21. protected $captchaPrivateKey;
  22. public function __construct(
  23. string $networkId,
  24. CoreApiService $coreApiService,
  25. LoggerInterface $logger,
  26. RequestStack $rq,
  27. string $captchaPrivateKey
  28. ) {
  29. $this->networkId = $networkId;
  30. $this->coreApiService = $coreApiService;
  31. $this->logger = $logger;
  32. $this->error = 0;
  33. $this->requestStack = $rq;
  34. $this->captchaPrivateKey = $captchaPrivateKey;
  35. }
  36. public function postContact($body)
  37. {
  38. $url = preg_replace(['/%1/'], [$this->networkId], $this->coreApiService->getApiCoreUrl() . self::SUPPORT_URL);
  39. $response = $this->coreApiService->call(
  40. CoreApiService::CALLER_TYPE_PUBLIC,
  41. 'POST',
  42. $url,
  43. $url,
  44. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  45. ['rawBody' => true],
  46. json_decode($body)
  47. );
  48. return $response->status === 200;
  49. }
  50. public function getCategories(string $operator = '')
  51. {
  52. $url = preg_replace(['/%1/'], [$this->networkId], $this->coreApiService->getApiCoreUrl() . self::SUPPORT_URL);
  53. $lang = strtoupper($this->requestStack->getCurrentRequest()->get('_locale'));
  54. $params = ['language' => $lang];
  55. if(!empty($operator)) {
  56. $params['operator'] = $operator;
  57. }
  58. $response = $this->coreApiService->call(
  59. CoreApiService::CALLER_TYPE_PUBLIC,
  60. 'GET',
  61. $url,
  62. $url,
  63. ['Accept' => 'application/json', 'Content-Type' => 'application/json'],
  64. $params
  65. );
  66. if($response->status === 200 && isset($response->body->supportCategories)) {
  67. return $response->body->supportCategories;
  68. } else {
  69. return [];
  70. }
  71. }
  72. public function postCaptchaToGoogle($gresponse)
  73. {
  74. $url = 'https://www.google.com/recaptcha/api/siteverify';
  75. $body = ['secret' => $this->captchaPrivateKey, 'response' => $gresponse];
  76. $response = $this->coreApiService->callRest(
  77. 'POST',
  78. $url,
  79. [],
  80. $body
  81. );
  82. return isset($response->body->success) ? $response->body->success : false;
  83. }
  84. }