| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of Symplify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Symplify\PHP7_Sculpin\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symplify\PHP7_Sculpin\Github\GihubPublishingProcess; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | final class PushToGithubCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $outputDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var GihubPublishingProcess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $gihubPublishingProcess; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct(string $outputDirectory, GihubPublishingProcess $gihubPublishingProcess) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->outputDirectory = $outputDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->gihubPublishingProcess = $gihubPublishingProcess; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->setName('push-to-github'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->setDescription('Push generated site to Github pages.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             'repository-slug', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             InputArgument::REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'Repository slug, e.g. "TomasVotruba/tomasvotruba.cz".' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->addOption('token', null, InputOption::VALUE_REQUIRED, 'Github token.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $this->ensureInputIsValid($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $this->gihubPublishingProcess->setupTravisIdentityToGit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $githubRepository = $this->createGithubRepositoryUrlWithToken( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $input->getOption('token'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 $input->getArgument('repository-slug') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $this->gihubPublishingProcess->addAndPushContentToGithubRepository( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $this->outputDirectory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $githubRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $output->writeln('<info>Website was successfully pushed to Github pages.</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } catch (Throwable $throwable) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $output->writeln( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 sprintf('<error>%s</error>', $throwable->getMessage()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     private function ensureInputIsValid(InputInterface $input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->ensureTokenOptionIsSet((string)$input->getOption('token')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->ensureGithubRepositorySlugIsValid($input->getArgument('repository-slug')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     private function ensureTokenOptionIsSet(string $token) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if ($token === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             throw new \Exception('Set token value via "--token=<GITHUB_TOKEN>" option.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     private function ensureGithubRepositorySlugIsValid(string $repositorySlug) | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $repositoryUrl = 'https://github.com/'.$repositorySlug; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         if (!$this->doesUrlExist($repositoryUrl)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             throw new \Exception(sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                 'Repository "%s" is not accessible. Try fixing the "%s" slug.', $repositoryUrl, $repositorySlug | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             )); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     private function doesUrlExist(string $url) : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $fileHeaders = @get_headers($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (!$fileHeaders || $fileHeaders[0] === 'HTTP/1.1 404 Not Found') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     private function createGithubRepositoryUrlWithToken(string  $token, string $repositorySlug) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             'https://%[email protected]/%s.git', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $token, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $repositorySlug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  |