| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace PhpCfdi\CfdiToPdf\Script; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use CfdiUtils\Cleaner\Cleaner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use CfdiUtils\Nodes\XmlNodeUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use CfdiUtils\XmlResolver\XmlResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use PhpCfdi\CfdiToPdf\Builders\Html2PdfBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use PhpCfdi\CfdiToPdf\CfdiDataBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PhpCfdi\CfdiToPdf\Converter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class ConvertScript | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param ConvertOptions $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function run(ConvertOptions $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $source = $this->openSource($options->inputFile(), $options->doCleanInput()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $comprobante = XmlNodeUtils::nodeFromXmlString($source); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $cfdiData = $this->createCfdiDataBuilder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             ->withXmlResolver($this->createXmlResolver($options->resolverLocation())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             ->build($comprobante); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $converter = $this->defaultConverter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $converter->createPdfAs($cfdiData, $options->outputFile()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 5 |  |     public function openSource(string $inputfile, bool $doCleanInput): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 5 |  |         if ('' === $inputfile) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |             throw new RuntimeException('Did not provide an input file'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 4 |  |         $filename = (string) realpath($inputfile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 4 |  |         if ('' === $filename) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |             throw new RuntimeException("The file $inputfile does not exists"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |         if (! is_file($filename)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |             throw new RuntimeException("The path $inputfile is not a file"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         /** @noinspection PhpUsageOfSilenceOperatorInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |         $source = strval(@file_get_contents($filename)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |         if ('' === $source) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             throw new RuntimeException("The file $inputfile is empty"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |         if ($doCleanInput) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |             $source = $this->cleanSource($source); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         return $source; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     public function cleanSource(string $source): string | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return Cleaner::staticClean($source); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function createXmlResolver(string $resolverLocation): XmlResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return new XmlResolver($resolverLocation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function createCfdiDataBuilder(): CfdiDataBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return new CfdiDataBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function defaultConverter(): Converter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return new Converter(new Html2PdfBuilder()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  |