| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace iio\libmergepdf\Driver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use iio\libmergepdf\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use iio\libmergepdf\Source\SourceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use setasign\Fpdi\Fpdi as FpdiFpdf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use setasign\Fpdi\Tcpdf\Fpdi as FpdiTcpdf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use setasign\Fpdi\PdfParser\StreamReader; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | final class Fpdi2Driver implements DriverInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * @var FpdiFpdf|FpdiTcpdf | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     private $fpdi; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @param FpdiFpdf|FpdiTcpdf $fpdi | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function __construct($fpdi = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->fpdi = $fpdi ?: new FpdiTcpdf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if (!($this->fpdi instanceof FpdiFpdf) && !($this->fpdi instanceof FpdiTcpdf)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             throw new \InvalidArgumentException('Constructor argument must be an FPDI instance.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 32 |  | View Code Duplication |     public function merge(SourceInterface ...$sources): string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $sourceName = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $fpdi = clone $this->fpdi; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             foreach ($sources as $source) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 $sourceName = $source->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 $pageCount = $fpdi->setSourceFile(StreamReader::createByString($source->getContents())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 $pageNumbers = $source->getPages()->getPageNumbers() ?: range(1, $pageCount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 foreach ($pageNumbers as $pageNr) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     $template = $fpdi->importPage($pageNr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                     $size = $fpdi->getTemplateSize($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     $fpdi->AddPage( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                         $size['width'] > $size['height'] ? 'L' : 'P', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                         [$size['width'], $size['height']] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     $fpdi->useTemplate($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             return $fpdi->Output('', 'S'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             throw new Exception("'{$e->getMessage()}' in '$sourceName'", 0, $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 60 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |  | 
            
                        
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.