48 lines
1.5 KiB
PHP
48 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Companies;
|
|
|
|
use App\Services\Security\EncryptionService;
|
|
use App\Modules\Companies\CompanyModel;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
final class CompanyService
|
|
{
|
|
public function __construct(
|
|
private readonly CompanyModel $companyModel,
|
|
private readonly EncryptionService $encryption
|
|
) {}
|
|
|
|
public function createCompany(array $data): string
|
|
{
|
|
if (!isset($data['id'])) {
|
|
$data['id'] = Uuid::uuid4()->toString();
|
|
}
|
|
// Encrypt sensitive JoFotara credentials
|
|
if (isset($data['jofotara_client_id'])) {
|
|
$data['jofotara_client_id_encrypted'] = $this->encryption->encrypt($data['jofotara_client_id']);
|
|
unset($data['jofotara_client_id']);
|
|
}
|
|
|
|
if (isset($data['jofotara_secret_key'])) {
|
|
$data['jofotara_secret_key_encrypted'] = $this->encryption->encrypt($data['jofotara_secret_key']);
|
|
unset($data['jofotara_secret_key']);
|
|
}
|
|
|
|
return (string)$this->companyModel->create($data);
|
|
}
|
|
|
|
public function getJoFotaraCredentials(string $companyId): array
|
|
{
|
|
$company = $this->companyModel->find($companyId);
|
|
if (!$company) return [];
|
|
|
|
return [
|
|
'clientId' => $company['jofotara_client_id_encrypted'] ? $this->encryption->decrypt($company['jofotara_client_id_encrypted']) : null,
|
|
'secretKey' => $company['jofotara_secret_key_encrypted'] ? $this->encryption->decrypt($company['jofotara_secret_key_encrypted']) : null,
|
|
];
|
|
}
|
|
}
|