| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Cocotte\DigitalOcean; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Cocotte\Console\Style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Darsyn\IP\IP; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | final class NetworkingConfigurator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @var DomainRecord | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private $domainRecord; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var Domain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $domain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var Style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $style; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     public function __construct(DomainRecord $domainRecord, Domain $domain, Style $style) | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $this->domainRecord = $domainRecord; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $this->domain = $domain; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $this->style = $style; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param HostnameCollection|Hostname[] $hostnames | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param IP $ip | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     public function configure(HostnameCollection $hostnames, IP $ip) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->style->veryVerbose('Configuring networking for all the hostnames supplied: '.$hostnames->toString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         foreach ($hostnames as $host) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $this->style->veryVerbose('Configuring '.$host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $this->configureDomain($host, $ip); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param HostnameCollection|Hostname[] $hostnames | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function remove(HostnameCollection $hostnames) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->style->veryVerbose('Removing networking for all the hostnames supplied: '.$hostnames->toString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         foreach ($hostnames as $host) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $this->style->veryVerbose('Removing '.$host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->removeDomainRecord($host); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     private function configureDomain(Hostname $hostname, IP $ip): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if (!$this->domain->exists($hostname)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->style->verbose( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 "Domain '{$hostname->toRoot()}' does not exist. Creating it and adding ". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 "{$hostname->toRoot()} with ip {$ip->getShortAddress()}" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $this->domain->create($hostname, $ip); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->configureDomainRecord($hostname, $ip); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     private function configureDomainRecord(Hostname $hostname, IP $ip): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if ($this->domainRecord->exists($hostname)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             if (!$this->domainRecord->isUpToDate($hostname, $ip)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $this->style->note( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     "Domain record '{$hostname}' exists. Updating its ip to {$ip->getShortAddress()}" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $this->domainRecord->update($hostname, $ip); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 $this->style->verbose("Domain record '{$hostname}' exists and its ip is up-to-date."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $this->style->verbose( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 "Domain record '{$hostname}' does not exist. Creating it with ip {$ip->getShortAddress()}" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $this->domainRecord->create($hostname, $ip); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     private function removeDomainRecord(Hostname $hostname): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         if ($this->domainRecord->exists($hostname)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $this->style->verbose("Removing domain record '{$hostname}'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $this->domainRecord->delete($hostname); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $this->style->verbose("Domain record '{$hostname}' was already removed"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 |  |  | } |