| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Cocotte\Template\StaticSite; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Cocotte\Console\OptionProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Cocotte\Console\Style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cocotte\Console\StyledInputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cocotte\DigitalOcean\DnsValidated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Cocotte\DigitalOcean\DnsValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Cocotte\DigitalOcean\Hostname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Cocotte\Environment\EnvironmentState; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Question\Question; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class StaticSiteHostnameOptionProvider implements OptionProvider, DnsValidated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var Style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var DnsValidator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $dnsValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct(Style $style, DnsValidator $dnsValidator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->style = $style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->dnsValidator = $dnsValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function option(EnvironmentState $environmentState): InputOption | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return new StyledInputOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $this->optionName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             InputOption::VALUE_REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $this->helpMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $environmentState->defaultValue(StaticSiteHostname::STATIC_SITE_HOSTNAME) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function validate(string $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $staticSiteHostname = new StaticSiteHostname(Hostname::parse($value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->dnsValidator->validateHost($staticSiteHostname->toHostname()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function helpMessage(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $this->style->optionHelp( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             "Static site hostname", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $this->style->hostnameHelp('website', 'mywebsite') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     public function question(): Question | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         return new Question( | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             $this->style->quittableQuestion("Choose a <options=bold>hostname for the site</> (e.g., mysite.mydomain.com)") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function onCorrectAnswer(string $answer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         // do nothing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function optionName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return StaticSiteHostname::OPTION_NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  | } |