| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Mhor\MediaInfo\Runner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Process\ProcessBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class MediaInfoCommandRunner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $filePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var ProcessBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $processBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var Process | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $processAsync = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $command = 'mediainfo'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $arguments = array('--OUTPUT=XML', '-f'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param string         $filePath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param array          $arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param ProcessBuilder $processBuilder | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 5 |  |     public function __construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $filePath, | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $command = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         array $arguments = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $processBuilder = null | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 5 |  |         $this->filePath = $filePath; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 5 |  |         if ($command !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 2 |  |             $this->command = $command; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 2 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 5 |  |         if ($arguments !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 3 |  |             $this->arguments = $arguments; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 3 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 5 |  |         if ($processBuilder === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 2 |  |             $this->processBuilder = ProcessBuilder::create() | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 2 |  |             ->setPrefix($this->command) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 2 |  |             ->setArguments($this->arguments); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 2 |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 3 |  |             $this->processBuilder = $processBuilder; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |     public function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |         $lc_ctype = setlocale(LC_CTYPE, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |         $this->processBuilder->add($this->filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |         $this->processBuilder->setEnv('LANG', $lc_ctype); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 2 |  |         $process = $this->processBuilder->getProcess(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |         $process->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |         if (!$process->isSuccessful()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             throw new \RuntimeException($process->getErrorOutput()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         return $process->getOutput(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * Asynchronously start mediainfo operation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Make call to MediaInfoCommandRunner::wait() afterwards to receive output. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |     public function start() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         $this->processBuilder->add($this->filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |         $this->processAsync = $this->processBuilder->getProcess(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         // just takes advantage of symfony's underlying Process framework | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         // process runs in background | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |         $this->processAsync->start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Blocks until call is complete. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @throws \Exception        If this function is called before start() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |     public function wait() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |         if ($this->processAsync == null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             throw new \Exception('You must run `start` before running `wait`'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         // blocks here until process completes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |         $this->processAsync->wait(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         if (!$this->processAsync->isSuccessful()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             throw new \RuntimeException($this->processAsync->getErrorOutput()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |         return $this->processAsync->getOutput(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  | 
            
                        
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..