2024-04-11 13:42:37 +00:00
< ? php
namespace App\Livewire\Project\Service ;
2025-10-16 15:07:32 +00:00
use App\Livewire\Concerns\SynchronizesModelData ;
2024-04-11 13:42:37 +00:00
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
{
2025-10-16 15:07:32 +00:00
use SynchronizesModelData ;
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-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-10-16 11:04:44 +00:00
$this -> application = ServiceApplication :: query () -> findOrFail ( $this -> applicationId );
$this -> authorize ( 'view' , $this -> application );
2025-10-16 15:07:32 +00:00
$this -> syncFromModel ();
2025-10-13 13:38:59 +00:00
}
2025-10-16 15:07:32 +00:00
protected function getModelBindings () : array
2025-10-13 13:38:59 +00:00
{
2025-10-16 15:07:32 +00:00
return [
'fqdn' => '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 ();
}
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-10-13 13:38:59 +00:00
// Sync to model for domain conflict check
2025-10-16 15:07:32 +00:00
$this -> syncToModel ();
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 ();
2025-10-13 13:38:59 +00:00
$this -> application -> refresh ();
2025-10-21 06:47:38 +00:00
$this -> syncFromModel ();
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-10-21 06:47:38 +00:00
$this -> syncFromModel ();
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' );
}
}