2024-04-11 13:42:37 +00:00
< ? php
namespace App\Livewire\Project\Service ;
use App\Models\ServiceApplication ;
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
{
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 ;
2024-04-11 13:42:37 +00:00
protected $rules = [
'application.fqdn' => 'nullable' ,
'application.required_fqdn' => 'required|boolean' ,
];
2024-06-10 20:43:34 +00:00
public function mount ()
{
2025-01-07 14:31:43 +00:00
$this -> application = ServiceApplication :: find ( $this -> applicationId );
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 ();
}
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 {
$this -> application -> fqdn = str ( $this -> application -> fqdn ) -> replaceEnd ( ',' , '' ) -> trim ();
$this -> application -> fqdn = str ( $this -> application -> fqdn ) -> replaceStart ( ',' , '' ) -> trim ();
$this -> application -> fqdn = str ( $this -> application -> 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
});
$this -> application -> fqdn = $this -> application -> fqdn -> unique () -> implode ( ',' );
2024-10-20 20:15:31 +00:00
$warning = sslipDomainWarning ( $this -> application -> fqdn );
if ( $warning ) {
$this -> dispatch ( 'warning' , __ ( 'warning.sslipdomain' ));
}
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 ;
}
2024-04-11 13:42:37 +00:00
$this -> validate ();
$this -> application -> save ();
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' );
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 ;
}
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' );
}
}