| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of graze/data-file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license https://github.com/graze/data-file/blob/master/LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @link    https://github.com/graze/data-file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Graze\DataFile\Modify; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Graze\DataFile\Helper\GetOptionTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Graze\DataFile\Helper\OptionalLoggerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Graze\DataFile\Helper\Process\ProcessFactoryAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Graze\DataFile\Node\FileNodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Graze\DataFile\Node\LocalFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Psr\Log\LoggerAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Psr\Log\LogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\Process\Exception\ProcessFailedException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 26 |  | View Code Duplication | class Tail implements FileModifierInterface, LoggerAwareInterface, ProcessFactoryAwareInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     use OptionalLoggerTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     use FileProcessTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     use GetOptionTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $lines; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Can this file be modified by this modifier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param FileNodeInterface $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |     public function canModify(FileNodeInterface $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |         return (($file instanceof LocalFile) && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |             ($file->exists())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Modify the file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param FileNodeInterface $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param array             $options List of options: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      *                                   -lines <string> Number of lines to tail (accepts +/- modifiers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      *                                   -postfix <string> (Default: replace) Set this to blank to replace inline | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      *                                   -keepOldFile <bool> (Default: true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return FileNodeInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 4 |  |     public function modify(FileNodeInterface $file, array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 4 |  |         $this->options = $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 4 |  |         $lines = $this->requireOption('lines'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |         unset($options['lines']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |         if (!($file instanceof LocalFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |             throw new InvalidArgumentException("Supplied: $file is not a LocalFile"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 2 |  |         return $this->tail($file, $lines, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Tail a file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param LocalFile $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param string    $lines         Number of lines to tail (accepts +/- modifiers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param array     $options       List of options: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      *                                 -postfix <string> (Default: tail) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      *                                 -keepOldFile <bool> (Default: true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @throws ProcessFailedException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return LocalFile | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 86 | 8 |  |     public function tail(LocalFile $file, $lines, array $options = []) | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 88 | 8 |  |         $this->options = $options; | 
            
                                                        
            
                                    
            
            
                | 89 | 8 |  |         $this->lines = $lines; | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 | 8 |  |         $postfix = $this->getOption('postfix', 'tail'); | 
            
                                                        
            
                                    
            
            
                | 92 | 8 |  |         $output = $this->getTargetFile($file, $postfix); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 | 8 |  |         $this->log(LogLevel::INFO, "Retrieving the last {lines} from file {file}", [ | 
            
                                                        
            
                                    
            
            
                | 95 | 8 |  |             'lines' => $this->lines, | 
            
                                                        
            
                                    
            
            
                | 96 | 8 |  |             'file'  => $file, | 
            
                                                        
            
                                    
            
            
                | 97 | 8 |  |         ]); | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 | 8 |  |         $cmd = sprintf('tail -n %s %s > %s', $this->lines, $file->getPath(), $output->getPath()); | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 101 | 8 |  |         return $this->processFile($file, $output, $cmd, $this->getOption('keepOldFile', true)); | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                        
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.