117 lines
3.3 KiB
PHP
117 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Integracao\Cns;
|
|
|
|
use App\Exceptions\Integracao\Cns\CertificadoDigitalInvalidoException;
|
|
use App\Exceptions\Integracao\Cns\SenhaInvalidaException;
|
|
use App\Models\Integracao\Cns\Parametrizacao;
|
|
use App\Services\Integracao\CnsService;
|
|
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_cns_parametrizacao')
|
|
->select([
|
|
'id',
|
|
DB::raw("'" . CnsService::getFile() . "' AS certificado_digital"),
|
|
])
|
|
->get();
|
|
}
|
|
|
|
public static function create()
|
|
{
|
|
return [
|
|
'model' => [
|
|
'certificado_digital' => null,
|
|
'senha' => null,
|
|
],
|
|
];
|
|
}
|
|
|
|
public static function store($request)
|
|
{
|
|
self::validate($request);
|
|
|
|
DB::transaction(function() use ($request) {
|
|
Parametrizacao::create(['senha' => $request->senha]);
|
|
|
|
$pkcs12 = openssl_pkcs12_read(file_get_contents($request->files->get('certificado_digital')), $certificates, $request->senha);
|
|
|
|
if ($pkcs12 ) {
|
|
$path = CnsService::getPath() . DIRECTORY_SEPARATOR . CnsService::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 = CnsService::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 destroy()
|
|
{
|
|
DB::transaction(function() {
|
|
self::show()->delete();
|
|
|
|
$path = CnsService::getPath() . DIRECTORY_SEPARATOR . CnsService::getFile();
|
|
|
|
if (Storage::disk('local')->exists($path)) {
|
|
Storage::disk('local')->delete($path);
|
|
}
|
|
});
|
|
}
|
|
|
|
public static function download()
|
|
{
|
|
$path = 'documento/integracao/cns/evidencias.pdf';
|
|
|
|
if (Storage::disk('local')->exists($path)) {
|
|
return [
|
|
'arquivo' => Storage::path($path),
|
|
'nome' => 'EVIDENCIAS.PDF',
|
|
];
|
|
} else {
|
|
throw new Exception('Arquivo não encontrado');
|
|
}
|
|
}
|
|
|
|
private static function validate()
|
|
{
|
|
request()->validate([
|
|
'certificado_digital' => [
|
|
'required',
|
|
],
|
|
'senha' => [
|
|
'required',
|
|
'string',
|
|
'max:180',
|
|
],
|
|
]);
|
|
}
|
|
}
|