181 lines
5.2 KiB
PHP
181 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Integracao\Rira;
|
|
|
|
use App\Exceptions\Integracao\Rira\CertificadoDigitalInvalidoException;
|
|
use App\Exceptions\Integracao\Rira\CertificadoDigitalNaoEncontradoException;
|
|
use App\Exceptions\Integracao\Rira\SenhaInvalidaException;
|
|
use App\Models\Integracao\Rira\Parametrizacao;
|
|
use App\Rules\Cns;
|
|
use App\Services\Integracao\RiraService;
|
|
use App\Services\Service;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class ParametrizacaoService extends Service
|
|
{
|
|
public static function index()
|
|
{
|
|
return DB::table('integracao_rira_parametrizacao')
|
|
->select([
|
|
'id',
|
|
DB::raw("'" . RiraService::getFile() . "' AS certificado_digital"),
|
|
'identificador_solicitante',
|
|
'cns_solicitante',
|
|
])
|
|
->get();
|
|
}
|
|
|
|
public static function create()
|
|
{
|
|
return [
|
|
'model' => [
|
|
'certificado_digital' => null,
|
|
'senha' => null,
|
|
'identificador_solicitante' => null,
|
|
'cns_solicitante' => null,
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function store($request)
|
|
{
|
|
self::validateStore($request);
|
|
|
|
DB::transaction(function() use ($request) {
|
|
Parametrizacao::create([
|
|
'senha' => $request->senha,
|
|
'identificador_solicitante' => $request->identificador_solicitante,
|
|
'cns_solicitante' => $request->cns_solicitante,
|
|
]);
|
|
|
|
$pkcs12 = openssl_pkcs12_read(file_get_contents($request->files->get('certificado_digital')), $certificates, $request->senha);
|
|
|
|
if ($pkcs12) {
|
|
$path = RiraService::getPath() . DIRECTORY_SEPARATOR . RiraService::getFile();
|
|
|
|
if (isset($certificates['cert']) && isset($certificates['pkey'])) {
|
|
$contents = $certificates['cert'].$certificates['pkey'];
|
|
} else {
|
|
throw new CertificadoDigitalInvalidoException();
|
|
}
|
|
|
|
if (!Storage::disk('local')->put($path, $contents)) {
|
|
throw new Exception('Erro ao fazer o upload do certificado.');
|
|
} else {
|
|
$token = RiraService::getToken($request->senha);
|
|
|
|
if (isset($token['access_token'])) {
|
|
return true;
|
|
} else {
|
|
throw new CertificadoDigitalInvalidoException();
|
|
}
|
|
}
|
|
} else {
|
|
throw new SenhaInvalidaException();
|
|
}
|
|
});
|
|
}
|
|
|
|
public static function show()
|
|
{
|
|
return Parametrizacao::first();
|
|
}
|
|
|
|
public static function edit()
|
|
{
|
|
$parametrizacao = self::show();
|
|
|
|
return [
|
|
'model' => [
|
|
'certificado_digital' => null,
|
|
'senha' => str_repeat('*', strlen($parametrizacao->senha)),
|
|
'identificador_solicitante' => $parametrizacao->identificador_solicitante,
|
|
'cns_solicitante' => $parametrizacao->cns_solicitante,
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function update($request)
|
|
{
|
|
self::validateUpdate($request);
|
|
|
|
DB::transaction(function() use ($request) {
|
|
$parametrizacao = self::show();
|
|
|
|
$parametrizacao->update([
|
|
'identificador_solicitante' => $request->identificador_solicitante,
|
|
'cns_solicitante' => $request->cns_solicitante,
|
|
]);
|
|
});
|
|
}
|
|
|
|
public static function destroy()
|
|
{
|
|
DB::transaction(function() {
|
|
self::show()->delete();
|
|
|
|
$path = RiraService::getPath() . DIRECTORY_SEPARATOR . RiraService::getFile();
|
|
|
|
if (Storage::disk('local')->exists($path)) {
|
|
Storage::disk('local')->delete($path);
|
|
}
|
|
});
|
|
}
|
|
|
|
public static function download()
|
|
{
|
|
$path = 'documento/integracao/rira/evidencias.pdf';
|
|
|
|
if (Storage::disk('local')->exists($path)) {
|
|
return [
|
|
'arquivo' => Storage::path($path),
|
|
'nome' => 'EVIDENCIAS.PDF',
|
|
];
|
|
} else {
|
|
throw new CertificadoDigitalNaoEncontradoException();
|
|
}
|
|
}
|
|
|
|
private static function validateStore()
|
|
{
|
|
request()->validate([
|
|
'certificado_digital' => [
|
|
'required',
|
|
],
|
|
'senha' => [
|
|
'required',
|
|
'string',
|
|
'max:180',
|
|
],
|
|
'identificador_solicitante' => [
|
|
'required',
|
|
'string',
|
|
'max:100',
|
|
],
|
|
'cns_solicitante' => [
|
|
'required',
|
|
'digits:15',
|
|
new Cns,
|
|
],
|
|
]);
|
|
}
|
|
|
|
private static function validateUpdate()
|
|
{
|
|
request()->validate([
|
|
'identificador_solicitante' => [
|
|
'required',
|
|
'string',
|
|
'max:100',
|
|
],
|
|
'cns_solicitante' => [
|
|
'required',
|
|
'digits:15',
|
|
new Cns,
|
|
],
|
|
]);
|
|
}
|
|
}
|