| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Posprint\Connectors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Class File | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Create a binary file and writes the data line by line. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * And it can also be used to provide direct connections to USB ports. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @category  NFePHP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package   Posprint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @copyright Copyright (c) 2016 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @license   http://www.gnu.org/licenses/lesser.html LGPL v3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author    Roberto L. Machado <linux.rlm at gmail dot com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @link      http://github.com/nfephp-org/posprint for the canonical source repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Posprint\Connectors\ConnectorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class File implements ConnectorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var resource|bool The file pointer to send data to. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $resource = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Construct new connector, given a filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * If created a binary file must be granted the necessary | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * permissions to create and write the system file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param string|null $filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function __construct($filename = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 8 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         if (!is_null($filename) && empty($filename)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 8 |  |             throw new InvalidArgumentException("A filename must be passed!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if (!is_null($filename)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 7 |  |             $command = 'rb+'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 7 |  |             if (!is_file($filename)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |                 $command = 'wb+'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 7 |  |             $this->resource = @fopen($filename, $command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 7 |  |             if ($this->resource === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |                 throw new RuntimeException("Failed to open the file. Check the permissions!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Destruct conection closing the file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 6 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function __destruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 5 |  |         if ($this->resource != false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 5 |  |             $this->close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |        | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Close file pointer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 6 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 6 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 6 |  |         if (is_resource($this->resource)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             if (! @fclose($this->resource)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 //when a fclose returns false ?? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 6 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 6 |  |         $this->resource = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * Write data to the file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |     public function write($data = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |         if (is_resource($this->resource) && !empty($data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |             return (int) fwrite($this->resource, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Read some bytes from file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param  int $len | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 93 | 2 |  |     public function read($len = null) | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 95 | 2 |  |         $data = ''; | 
            
                                                        
            
                                    
            
            
                | 96 | 2 |  |         if (!is_resource($this->resource)) { | 
            
                                                        
            
                                    
            
            
                | 97 | 1 |  |             return $data; | 
            
                                                        
            
                                    
            
            
                | 98 | 1 |  |         } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         if (!is_null($len) && is_numeric($len)) { | 
            
                                                        
            
                                    
            
            
                | 100 | 1 |  |             $len = ceil($len); | 
            
                                                        
            
                                    
            
            
                | 101 | 1 |  |             return fread($this->resource, $len); | 
            
                                                        
            
                                    
            
            
                | 102 | 1 |  |         } | 
            
                                                        
            
                                    
            
            
                | 103 | 1 |  |         while (!feof($this->resource)) { | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |             $data .= fread($this->resource, 4096); | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |         return $data; | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  | 
            
                        
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.