22
33namespace App \Commands \Concerns ;
44
5+ use App \Traits \DomainSuggestions ;
56use function Laravel \Prompts \select ;
67
78trait InteractWithSite
89{
10+ use DomainSuggestions;
11+
912 protected function getServerAndSite (): array
1013 {
11- if ($ this ->hasPloiConfigurationFile () && ! $ this ->option ('server ' ) && ! $ this ->option ('site ' )) {
14+ if ($ this ->hasPloiConfigurationFile () &&
15+ !$ this ->option ('server ' ) &&
16+ !$ this ->option ('site ' )) {
1217 $ serverId = $ this ->configuration ->get ('settings.server ' );
1318 $ siteId = $ this ->configuration ->get ('settings.site ' );
1419 } else {
15- $ serverIdentifier = $ this ->option ('server ' ) ?? $ this ->selectServer ();
20+ $ serverIdentifier = $ this ->option ('server ' )
21+ ?? $ this ->selectServer ();
1622 $ serverId = $ this ->getServerIdByNameOrIp ($ serverIdentifier );
1723
18- $ siteIdentifier = $ this ->option ('site ' ) ?? $ this ->selectSite ($ serverId )['domain ' ];
24+ $ siteIdentifier = $ this ->option ('site ' )
25+ ?? $ this ->selectSite ($ serverId )['domain ' ];
1926 $ siteId = $ this ->getSiteIdByDomain ($ serverId , $ siteIdentifier );
2027 }
2128
22- if (! $ serverId || ! $ siteId ) {
29+ if (!$ serverId || !$ siteId ) {
2330 $ this ->error ('Server and site must be valid. ' );
2431 exit (1 );
2532 }
@@ -29,7 +36,9 @@ protected function getServerAndSite(): array
2936
3037 protected function selectSite ($ serverId ): array
3138 {
32- $ sites = collect ($ this ->ploi ->getSiteList ($ serverId )['data ' ])->pluck ('domain ' , 'domain ' )->toArray ();
39+ $ sites = collect ($ this ->ploi ->getSiteList ($ serverId )['data ' ])
40+ ->pluck ('domain ' , 'domain ' )
41+ ->toArray ();
3342 $ domain = select ('Select a site by domain: ' , $ sites , scroll: 10 );
3443
3544 return ['domain ' => $ domain ];
@@ -38,12 +47,19 @@ protected function selectSite($serverId): array
3847 protected function getSiteIdByDomain (int $ serverId , string $ domain ): ?int
3948 {
4049 $ sites = collect ($ this ->ploi ->getSiteList ($ serverId )['data ' ]);
50+ $ availableDomains = $ sites ->pluck ('domain ' )->toArray ();
4151
4252 $ site = $ sites ->first (fn ($ site ) => $ site ['domain ' ] === $ domain );
4353
44- if (! $ site ) {
45- $ this ->error ("Site with domain ' {$ domain }' not found on the selected server. " );
46- exit (1 );
54+ if (!$ site ) {
55+ $ selectedDomain = $ this ->getDomainSuggestionsWithSelection ($ domain , $ availableDomains );
56+
57+ if ($ selectedDomain ) {
58+ $ site = $ sites ->first (fn ($ site ) => $ site ['domain ' ] === $ selectedDomain );
59+ } else {
60+ $ this ->error ("Site with domain ' {$ domain }' not found on the selected server. " );
61+ exit (1 );
62+ }
4763 }
4864
4965 return $ site ['id ' ];
0 commit comments