You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

169 lines
5.3 KiB

<?php
require_once CONF_RAIZ_PROJETO ."/core/Integracao.php";
class ProvedorCase extends Integracao
{
private IProvedor $provedor;
private string $intVersao;
public function __construct(string $uid)
{
$this->setLog(CONF_LOGGER_ATIVO);
$this->data();
$this->setVersaoIntegracao($uid);
$apiConfig = $this->handle($uid);
$this->autoload($apiConfig);
$this->authProvedor($apiConfig, $this);
$this->integracaoReg();
}
private function handle(string $uid): array
{
$apiConfig = $this->getConfigIntegracao($uid);
return $apiConfig;
}
public function getProvedor(): IProvedor
{
return $this->provedor;
}
private function getConfigIntegracao(string $uniqueid)
{
$response = $this->db()->getInfoIntegracaoURA($uniqueid);
if (!$response) {
$this->log()->error("Nao foi possivel recuperar as credenciais de acesso", debug_backtrace());
$this->audioError(CONF_AUDIO_ERROR);
return false;
}
$this->setDataIntAgi([
"sistema_integracao" => $response['name']
]);
return $response;
}
private function authProvedor(array $apiConfig, ProvedorCase $case): void
{
$this->provedor->autenticar($apiConfig, $case);
}
public function custom(string $func, array $params): void
{
$this->provedor->$func($params);
}
private function setVersaoIntegracao(string $uid){
$result = $this->db()->checkUniqueidType($uid);
$direcao = $result['direcao'];
$verifyString = stripos($direcao, "ura");
$isUra = $verifyString !== false;
if($isUra){
$uraNome = $this->db()->getUraMovimentoByUniqueid($uid)['umv_ura_nome'];
$findIndexIntegracao = strpos($uraNome, "_INT");
$containsIntegracao = (bool) $findIndexIntegracao;
if(!$containsIntegracao){
$this->log()->error("A URA nao possui vers<EFBFBD>o", debug_backtrace());
$this->audioError(CONF_AUDIO_ERROR);
return false;
}
$this->intVersao = substr($uraNome, $findIndexIntegracao);
}else{
$anuncioId = substr($result['destino'], 1);
$anuncioNome = $this->db()->getAnuncioNameById($anuncioId);
$findIndexIntegracao = strpos($anuncioNome, "_INT");
$containsIntegracao = (bool) $findIndexIntegracao;
if(!$containsIntegracao){
$this->log()->error("O Anuncio nao possui vers<EFBFBD>o", debug_backtrace());
$this->audioError(CONF_AUDIO_ERROR);
return false;
}
$this->intVersao = substr($anuncioNome, $findIndexIntegracao);
}
}
public function getIntVersion()
{
return $this->intVersao;
}
public function setDataIntAgi(array $data) : void
{
$merge = $this->getDataIntAgi();
if ($merge){
$data = array_merge($data, $merge);
}
$this->prepareDataToSaveClient($data);
$this->agi()->set_variable('INT_DATA', serialize($data));
}
private function prepareDataToSaveClient($data)
{
$documentoCliente = $data["documento"];
if (!$documentoCliente){
return false;
}
$preparedData = array(
$data["id_cliente"],
$data["sistema_integracao"],
$data["nome_cliente"],
$documentoCliente = Helpers::extrairNumeros($documentoCliente),
$tipoDocumentoCliente = strlen($documentoCliente) > 11 ? "CNPJ" : "CPF",
$data["telefone"],
$data["email"],
$data["cep"],
$data["cidade"],
$data["uf"]
);
$this->saveDataCliente($preparedData);
}
private function saveDataCliente($data)
{
$documentoCliente = $data[3];
$response = $this->db()->existeCliente($documentoCliente);
$existeCliente = $response['int_id_cliente'];
if (!$existeCliente){
$this->db()->saveDataIntCliente($data);
} else {
$this->db()->updateDataIntCliente($data);
}
}
public function getDataIntAgi() : array | bool
{
$data = $this->agi()->get_variable("INT_DATA", true);
if ($data){
return unserialize($data);
}
return false;
}
private function autoload(array $apiConfig): void
{
$providers = $this->load();
$providerName = $apiConfig["name"];
$this->provedor = $providers[$providerName];
}
private function load(): array
{
return require_once CONF_RAIZ_PROJETO . "/config/provedores.php";
}
}