82 lines
3.7 KiB
PHP
82 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Http\Controllers\InvestmentPortfolioController;
|
|
use App\Models\ExistingBusiness;
|
|
use App\Models\Opportunity;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
class PartnershipSources
|
|
{
|
|
/** @return array<string,mixed> */
|
|
public function resolve(string $type, string $id, ?User $actor = null): array
|
|
{
|
|
$result = ['type' => $type, 'target_id' => $id, 'geography' => null, 'public' => true];
|
|
if (in_array($type, ['project', 'cluster', 'dossier', 'zone', 'business', 'company', 'simulation'], true)) {
|
|
abort_unless(Str::isUuid($id), 422, 'Identificador da origem inválido.');
|
|
}
|
|
if (in_array($type, ['project', 'cluster', 'dossier'], true)) {
|
|
$dossier = InvestmentPortfolioController::visible()->where($type === 'dossier' ? 'id' : $type.'_id', $id)->first();
|
|
abort_unless($dossier !== null, 404, 'Dossier público de origem não disponível.');
|
|
|
|
return [...$result, 'title' => $dossier->title, 'href' => '/carteira/'.$dossier->id, 'geography' => app(InvestmentRelations::class)->dossier($dossier)];
|
|
}
|
|
if ($type === 'investment') {
|
|
abort_unless(ctype_digit($id), 422);
|
|
$record = Opportunity::query()->published()->whereKey((int) $id)->first();
|
|
abort_unless($record !== null, 404);
|
|
|
|
return [...$result, 'title' => $record->title, 'href' => '/oportunidades/'.$record->slug];
|
|
}
|
|
if ($type === 'zone') {
|
|
$record = DB::table('investment_zones')->where('id', $id)->where('publication_status', 'published')->whereNull('deleted_at')->first();
|
|
abort_unless($record !== null, 404);
|
|
|
|
return [...$result, 'title' => $record->name, 'href' => '/zonas/'.$id];
|
|
}
|
|
if ($type === 'business') {
|
|
$record = ExistingBusiness::query()->published()->whereKey($id)->first();
|
|
abort_unless($record !== null, 404);
|
|
|
|
return [...$result, 'title' => $record->public_title, 'href' => '/negocios/'.$record->public_slug];
|
|
}
|
|
if ($type === 'company') {
|
|
abort_unless($actor !== null, 403);
|
|
$record = DB::table('investment_company_declarations')->where('id', $id)->where('user_id', $actor->id)->first();
|
|
abort_unless($record !== null, 404);
|
|
|
|
return [...$result, 'title' => 'Declaração empresarial existente', 'href' => '/investidor/profile', 'public' => false];
|
|
}
|
|
if ($type === 'simulation') {
|
|
abort_unless($actor !== null, 403);
|
|
$record = DB::table('simulation_form_drafts')->where('id', $id)->where('owner_user_id', $actor->id)->whereNull('deleted_at')->first();
|
|
abort_unless($record !== null, 404);
|
|
|
|
return [...$result, 'title' => 'Ficha de projeto do simulador', 'href' => '/simulador-fiscal?draft='.$id, 'public' => false];
|
|
}
|
|
abort(422, 'Tipo de origem não suportado.');
|
|
}
|
|
|
|
/** @return list<array<string,mixed>> */
|
|
public function forPartnership(int $id, ?User $actor = null): array
|
|
{
|
|
$result = [];
|
|
foreach (DB::table('partnership_origins')->where('partnership_id', $id)->get() as $origin) {
|
|
try {
|
|
$result[] = $this->resolve($origin->type, $origin->target_id, $actor);
|
|
} catch (HttpException $e) {
|
|
if (! in_array($e->getStatusCode(), [403, 404], true)) {
|
|
throw $e;
|
|
}
|
|
// Private or withdrawn source identifiers are not disclosed by a public proposal.
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|