2024-04-11 13:42:37 +00:00
< ? php
namespace App\Livewire\Project\Service ;
use App\Models\ServiceApplication ;
2026-07-02 15:47:04 +00:00
use App\Support\ValidationPatterns ;
2025-11-04 08:18:05 +00:00
use Illuminate\Foundation\Auth\Access\AuthorizesRequests ;
use Livewire\Attributes\Validate ;
2024-04-11 13:42:37 +00:00
use Livewire\Component ;
class EditDomain extends Component
{
2025-11-04 08:18:05 +00:00
use AuthorizesRequests ;
2024-04-11 13:42:37 +00:00
public $applicationId ;
2024-06-10 20:43:34 +00:00
2024-04-11 13:42:37 +00:00
public ServiceApplication $application ;
2024-06-10 20:43:34 +00:00
2025-08-28 08:52:41 +00:00
public $domainConflicts = [];
public $showDomainConflictModal = false ;
public $forceSaveDomains = false ;
2025-11-06 13:30:39 +00:00
public $showPortWarningModal = false ;
public $forceRemovePort = false ;
public $requiredPort = null ;
2026-07-02 15:47:04 +00:00
#[Validate]
2025-10-13 13:38:59 +00:00
public ? string $fqdn = null ;
2026-07-02 15:47:04 +00:00
protected function rules () : array
{
return [
'fqdn' => ValidationPatterns :: applicationDomainRules (),
];
}
2024-06-10 20:43:34 +00:00
public function mount ()
{
2025-11-04 08:18:05 +00:00
$this -> application = ServiceApplication :: ownedByCurrentTeam () -> findOrFail ( $this -> applicationId );
2025-10-16 11:04:44 +00:00
$this -> authorize ( 'view' , $this -> application );
2025-11-10 13:15:53 +00:00
$this -> requiredPort = $this -> application -> getRequiredPort ();
2025-11-04 08:18:05 +00:00
$this -> syncData ();
2025-10-13 13:38:59 +00:00
}
2026-09-01 16:44:18 +00:00
private function syncData ( bool $toModel = false ) : void
2025-10-13 13:38:59 +00:00
{
2025-11-04 08:18:05 +00:00
if ( $toModel ) {
$this -> validate ();
// Sync to model
2026-09-02 17:39:19 +00:00
$this -> application -> setEditableUrls ( $this -> fqdn );
2025-11-04 08:18:05 +00:00
$this -> application -> save ();
} else {
// Sync from model
2026-09-02 17:39:19 +00:00
$this -> fqdn = $this -> application -> url ;
2025-11-04 08:18:05 +00:00
}
2024-04-11 13:42:37 +00:00
}
2024-10-17 20:08:23 +00:00
2025-08-28 08:52:41 +00:00
public function confirmDomainUsage ()
{
$this -> forceSaveDomains = true ;
$this -> showDomainConflictModal = false ;
$this -> submit ();
}
2025-11-06 13:30:39 +00:00
public function confirmRemovePort ()
{
$this -> forceRemovePort = true ;
$this -> showPortWarningModal = false ;
$this -> submit ();
}
public function cancelRemovePort ()
{
$this -> showPortWarningModal = false ;
$this -> syncData (); // Reset to original FQDN
}
2024-10-11 08:14:27 +00:00
public function submit ()
2024-04-11 13:42:37 +00:00
{
2024-10-10 08:24:11 +00:00
try {
2026-09-02 17:39:19 +00:00
$persistedApplication = $this -> application -> fresh ();
$previousEditableUrls = $persistedApplication -> url ;
$previousFqdn = $persistedApplication -> fqdn ;
$previousPortOverrides = $persistedApplication -> domain_port_overrides ;
2025-10-16 11:04:44 +00:00
$this -> authorize ( 'update' , $this -> application );
2026-07-02 15:47:04 +00:00
$this -> validate ();
$this -> fqdn = ValidationPatterns :: normalizeApplicationDomains ( $this -> fqdn );
2025-10-13 13:38:59 +00:00
$warning = sslipDomainWarning ( $this -> fqdn );
2024-10-20 20:15:31 +00:00
if ( $warning ) {
$this -> dispatch ( 'warning' , __ ( 'warning.sslipdomain' ));
}
2025-11-04 08:18:05 +00:00
// Sync to model for domain conflict check (without validation)
2026-09-02 17:39:19 +00:00
$this -> application -> setEditableUrls ( $this -> fqdn );
2025-08-28 08:52:41 +00:00
// Check for domain conflicts if not forcing save
if ( ! $this -> forceSaveDomains ) {
$result = checkDomainUsage ( resource : $this -> application );
if ( $result [ 'hasConflicts' ]) {
$this -> domainConflicts = $result [ 'conflicts' ];
$this -> showDomainConflictModal = true ;
return ;
}
} else {
// Reset the force flag after using it
$this -> forceSaveDomains = false ;
}
2025-11-06 13:30:39 +00:00
// Check for required port
if ( ! $this -> forceRemovePort ) {
2025-11-10 13:15:53 +00:00
$requiredPort = $this -> application -> getRequiredPort ();
2025-11-06 13:30:39 +00:00
if ( $requiredPort !== null ) {
2026-09-02 17:39:19 +00:00
foreach ( str ( $this -> fqdn ) -> trim () -> explode ( ',' ) as $fqdn ) {
$fqdn = trim (( string ) $fqdn );
if ( $fqdn === '' ) {
2025-11-06 13:30:39 +00:00
continue ;
}
2026-09-02 17:39:19 +00:00
if ( $this -> application -> portRequiresConfirmation ( $fqdn , $requiredPort , $previousEditableUrls )) {
$this -> requiredPort = $requiredPort ;
$this -> showPortWarningModal = true ;
$this -> application -> fqdn = $previousFqdn ;
$this -> application -> domain_port_overrides = $previousPortOverrides ;
2025-11-06 13:30:39 +00:00
2026-09-02 17:39:19 +00:00
return ;
}
2025-11-06 13:30:39 +00:00
}
}
} else {
// Reset the force flag after using it
$this -> forceRemovePort = false ;
}
2024-04-11 13:42:37 +00:00
$this -> validate ();
$this -> application -> save ();
2025-10-13 13:38:59 +00:00
$this -> application -> refresh ();
2025-11-04 08:18:05 +00:00
$this -> syncData ();
2024-04-11 13:42:37 +00:00
updateCompose ( $this -> application );
if ( str ( $this -> application -> fqdn ) -> contains ( ',' )) {
$this -> dispatch ( 'warning' , 'Some services do not support multiple domains, which can lead to problems and is NOT RECOMMENDED.<br><br>Only use multiple domains if you know what you are doing.' );
}
2024-04-16 10:41:21 +00:00
$this -> application -> service -> parse ();
2024-04-11 13:42:37 +00:00
$this -> dispatch ( 'refresh' );
2025-10-14 08:12:36 +00:00
$this -> dispatch ( 'refreshServices' );
2024-04-12 10:44:49 +00:00
$this -> dispatch ( 'configurationChanged' );
2025-01-07 14:31:43 +00:00
} catch ( \Throwable $e ) {
2024-10-11 08:14:27 +00:00
$originalFqdn = $this -> application -> getOriginal ( 'fqdn' );
if ( $originalFqdn !== $this -> application -> fqdn ) {
$this -> application -> fqdn = $originalFqdn ;
2025-11-04 08:18:05 +00:00
$this -> syncData ();
2024-10-11 08:14:27 +00:00
}
2024-10-17 20:08:23 +00:00
2024-10-11 08:14:27 +00:00
return handleError ( $e , $this );
2024-04-11 13:42:37 +00:00
}
}
2024-06-10 20:43:34 +00:00
2024-04-11 13:42:37 +00:00
public function render ()
{
return view ( 'livewire.project.service.edit-domain' );
}
}