| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace TreeHouse\IoBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\Common\Persistence\ManagerRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use TreeHouse\IoBundle\Entity\Import; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use TreeHouse\IoBundle\Entity\ImportRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use TreeHouse\IoBundle\Import\Log\ItemLoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class ImportLogViewCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var ManagerRegistry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $doctrine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var ItemLoggerInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $itemLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param ManagerRegistry     $doctrine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param ItemLoggerInterface $itemLogger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function __construct(ManagerRegistry $doctrine, ItemLoggerInterface $itemLogger) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->doctrine = $doctrine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->itemLogger = $itemLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->setName('io:import:log:view'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->addArgument('import', InputArgument::REQUIRED, 'The id of the import'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->setDescription('View an import log'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 53 |  | View Code Duplication |         if (null === $import = $this->findImportById($input->getArgument('import'))) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $output->writeln(sprintf('<error>Import %d does not exist</error>', $input->getArgument('import'))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $items = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $success = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $failed = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $skipped = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         foreach ($this->itemLogger->getImportedItems($import) as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $output->writeln(sprintf('<info>%s</info>:', $item['item'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             ++$items; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             foreach ($item as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 if ($key === 'item') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $output->writeln(sprintf('  <info>%s</info>: <comment>%s</comment>', $key, $value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             switch ($item['result']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 case 'success': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     $success++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 case 'failed': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     $failed++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 case 'skipped': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     $skipped++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $output->writeln(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $output->writeln(sprintf('Imported <info>%s</info> items', $items)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $output->writeln(sprintf('- succes:  <info>%s</info> (<info>%.2f%%</info>)', $success, $success / $items * 100)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $output->writeln(sprintf('- failed:  <info>%s</info> (<info>%.2f%%</info>)', $failed, $failed / $items * 100)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $output->writeln(sprintf('- skipped: <info>%s</info> (<info>%.2f%%</info>)', $skipped, $skipped / $items * 100)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param int $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @return Import | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     protected function findImportById($id) | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         return $this->getRepository()->find($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return ImportRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     protected function getRepository() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return $this->doctrine->getRepository('TreeHouseIoBundle:Import'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.