File: //var/www/quadcode/app/Helpers/RoistatHelper.php
<?php
namespace App\Helpers;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class RoistatHelper
{
private string $integrationHost;
private string $integrationToken;
private string $apiHost;
private int $apiProjectId;
private string $apiToken;
const LOG_CHANNEL = 'roistat';
public function __construct()
{
$this->integrationHost = env('ROISTAT_INTEGRATION_HOST');
$this->integrationToken = env('ROISTAT_INTEGRATION_TOKEN');
$this->apiHost = env('ROISTAT_API_HOST');
$this->apiProjectId = env('ROISTAT_API_PROJECT_ID');
$this->apiToken = env('ROISTAT_API_TOKEN');
}
public function check(): bool
{
return !empty($this->integrationHost) && !empty($this->integrationToken) && !empty($this->apiHost) && !empty($this->apiProjectId) && !empty($this->apiToken);
}
private function getIntegration(string $apiMethod, array $data = []): Response
{
$data['key'] = $this->integrationToken;
Log::channel(self::LOG_CHANNEL)->info('Integration get', compact('data'));
$response = Http::get($this->integrationHost . '/' . $apiMethod, $data);
Log::channel(self::LOG_CHANNEL)->info('Integration get response', $response->json());
return $response;
}
private function getApi(string $apiMethod, array $data = []): Response
{
$data['project'] = $this->apiProjectId;
Log::channel(self::LOG_CHANNEL)->info('API get', compact('data'));
$response = Http::withHeaders(['Api-key' => $this->apiToken])->get($this->apiHost . '/' . $apiMethod, $data);
Log::channel(self::LOG_CHANNEL)->info('API get response', $response->json());
return $response;
}
public function getLeads(array $data): Response
{
return $this->getApi('project/proxy-leads', $data);
}
public function add(array $data): Response
{
return $this->getIntegration('leads/add', $data);
}
}