2024-04-11 13:42:37 +00:00
< ? php
namespace App\Livewire\Project\Service ;
use App\Models\ServiceApplication ;
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 ;
2024-10-11 08:14:27 +00:00
use Spatie\Url\Url ;
2024-04-11 13:42:37 +00:00
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 ;
2025-11-04 08:18:05 +00:00
#[Validate(['nullable'])]
2025-10-13 13:38:59 +00:00
public ? string $fqdn = null ;
2024-04-11 13:42:37 +00:00
protected $rules = [
2025-10-13 13:38:59 +00:00
'fqdn' => 'nullable' ,
2024-04-11 13:42:37 +00:00
];
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
}
2025-11-04 08:18:05 +00:00
public 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
$this -> application -> fqdn = $this -> fqdn ;
$this -> application -> save ();
} else {
// Sync from model
$this -> fqdn = $this -> application -> fqdn ;
}
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 {
2025-10-16 11:04:44 +00:00
$this -> authorize ( 'update' , $this -> application );
2025-10-13 13:38:59 +00:00
$this -> fqdn = str ( $this -> fqdn ) -> replaceEnd ( ',' , '' ) -> trim () -> toString ();
$this -> fqdn = str ( $this -> fqdn ) -> replaceStart ( ',' , '' ) -> trim () -> toString ();
$domains = str ( $this -> fqdn ) -> trim () -> explode ( ',' ) -> map ( function ( $domain ) {
2025-08-29 13:09:03 +00:00
$domain = trim ( $domain );
2024-10-11 08:14:27 +00:00
Url :: fromString ( $domain , [ 'http' , 'https' ]);
2024-10-17 20:08:23 +00:00
2025-08-29 13:09:03 +00:00
return str ( $domain ) -> lower ();
2024-10-10 08:24:11 +00:00
});
2025-10-13 13:38:59 +00:00
$this -> fqdn = $domains -> unique () -> implode ( ',' );
$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)
$this -> application -> fqdn = $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 ) {
// Check if all FQDNs have a port
$fqdns = str ( $this -> fqdn ) -> trim () -> explode ( ',' );
$missingPort = false ;
foreach ( $fqdns as $fqdn ) {
$fqdn = trim ( $fqdn );
if ( empty ( $fqdn )) {
continue ;
}
$port = ServiceApplication :: extractPortFromUrl ( $fqdn );
if ( $port === null ) {
$missingPort = true ;
break ;
}
}
if ( $missingPort ) {
$this -> requiredPort = $requiredPort ;
$this -> showPortWarningModal = true ;
return ;
}
}
} 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' );
}
}