This commit is contained in:
180
api/app/Services/Integracao/Ria/ParametrizacaoService.php
Normal file
180
api/app/Services/Integracao/Ria/ParametrizacaoService.php
Normal file
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Integracao\Ria;
|
||||
|
||||
use App\Exceptions\Integracao\Ria\CertificadoDigitalInvalidoException;
|
||||
use App\Exceptions\Integracao\Ria\CertificadoDigitalNaoEncontradoException;
|
||||
use App\Exceptions\Integracao\Ria\SenhaInvalidaException;
|
||||
use App\Models\Integracao\Ria\Parametrizacao;
|
||||
use App\Rules\Cns;
|
||||
use App\Services\Integracao\RiaService;
|
||||
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_ria_parametrizacao')
|
||||
->select([
|
||||
'id',
|
||||
DB::raw("'" . RiaService::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 = RiaService::getPath() . DIRECTORY_SEPARATOR . RiaService::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 = RiaService::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 = RiaService::getPath() . DIRECTORY_SEPARATOR . RiaService::getFile();
|
||||
|
||||
if (Storage::disk('local')->exists($path)) {
|
||||
Storage::disk('local')->delete($path);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function download()
|
||||
{
|
||||
$path = 'documento/integracao/ria/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,
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user