| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Ozean12\WebTranslateItBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Ozean12\WebTranslateItBundle\DTO\ProjectDTO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Ozean12\WebTranslateItBundle\DTO\ProjectFileDTO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Ozean12\WebTranslateItBundle\Service\FileServiceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Ozean12\WebTranslateItBundle\Service\TranslationRepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Stopwatch\Stopwatch; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class TranslationsPullCommand. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class TranslationsPullCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var TranslationRepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $translationRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var FileServiceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $fileService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $translationDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var Stopwatch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $stopwatch; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * TranslationsPullCommand constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param TranslationRepositoryInterface $translationRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param FileServiceInterface           $fileService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string                         $translationsDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         TranslationRepositoryInterface $translationRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         FileServiceInterface $fileService, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $translationsDirectory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->translationRepository = $translationRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->fileService = $fileService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->translationDirectory = $translationsDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         parent::__construct('ozean12:webtranslateit:pull'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ->setDescription('Pull latest translations from translation repository') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         $this->prepare($output); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $project = $this->translationRepository->pullProject()->getProject(); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $this->showWelcomeMessage($project, $output); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $totalFilesCount = $project->getProjectFiles()->count(); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         $currentFileCount = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         foreach ($project->getProjectFiles() as $projectFile) { | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             $this->processFile($projectFile, ++$currentFileCount, $totalFilesCount, $output); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $this->finish($output); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param ProjectDTO      $project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     private function showWelcomeMessage(ProjectDTO $project, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $output->writeln( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 'Pulling translations for <info>%s</info> into <info>%s</info>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $project->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 realpath($this->translationDirectory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             OutputInterface::VERBOSITY_VERBOSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     private function prepare(OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if ($output->isVerbose()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $this->stopwatch = new Stopwatch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $this->stopwatch->start('execute'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->fileService->prepare($this->translationDirectory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     private function finish(OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         if ($output->isVerbose()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $event = $this->stopwatch->stop('execute'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $output->writeln(sprintf('Translations updated in %ss', $event->getDuration() / 1000)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param ProjectFileDTO  $projectFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param int             $currentFileCount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @param int             $totalFilesCount | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     private function processFile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         ProjectFileDTO $projectFile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $currentFileCount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $totalFilesCount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $filePath = sprintf('%s/%s', $this->translationDirectory, $projectFile->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $shouldBeUpdated = $this->fileService->shouldBeUpdated($filePath, $projectFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $output->writeln( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                 '[%s / %s]: <info>%s</info> -> <info>%s</info>', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 $currentFileCount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 $totalFilesCount, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 $projectFile->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 $shouldBeUpdated ? 'pulling new version' : 'skipping' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             OutputInterface::VERBOSITY_VERBOSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         if ($shouldBeUpdated) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             $this->fileService->update($filePath, $projectFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |  |