Code Duplication    Length = 78-78 lines in 2 locations

src/Modify/Head.php 1 location

@@ 26-103 (lines=78) @@
23
use Psr\Log\LogLevel;
24
use Symfony\Component\Process\Exception\ProcessFailedException;
25
26
class Head 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
    public function canModify(FileNodeInterface $file)
45
    {
46
        return (($file instanceof LocalFile) &&
47
            ($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
    public function modify(FileNodeInterface $file, array $options = [])
62
    {
63
        $this->options = $options;
64
        $lines = $this->requireOption('lines');
65
        unset($options['lines']);
66
67
        if (!($file instanceof LocalFile)) {
68
            throw new InvalidArgumentException("Supplied: $file is not a LocalFile");
69
        }
70
71
        return $this->head($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
    public function head(LocalFile $file, $lines, array $options = [])
87
    {
88
        $this->options = $options;
89
        $this->lines = $lines;
90
91
        $postfix = $this->getOption('postfix', 'head');
92
        $output = $this->getTargetFile($file, $postfix);
93
94
        $this->log(LogLevel::INFO, "Retrieving the last {lines} from file {file}", [
95
            'lines' => $this->lines,
96
            'file'  => $file,
97
        ]);
98
99
        $cmd = sprintf('head -n %s %s > %s', $this->lines, $file->getPath(), $output->getPath());
100
101
        return $this->processFile($file, $output, $cmd, $this->getOption('keepOldFile', true));
102
    }
103
}
104

src/Modify/Tail.php 1 location

@@ 26-103 (lines=78) @@
23
use Psr\Log\LogLevel;
24
use Symfony\Component\Process\Exception\ProcessFailedException;
25
26
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
    public function canModify(FileNodeInterface $file)
45
    {
46
        return (($file instanceof LocalFile) &&
47
            ($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
    public function modify(FileNodeInterface $file, array $options = [])
62
    {
63
        $this->options = $options;
64
        $lines = $this->requireOption('lines');
65
        unset($options['lines']);
66
67
        if (!($file instanceof LocalFile)) {
68
            throw new InvalidArgumentException("Supplied: $file is not a LocalFile");
69
        }
70
71
        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
    public function tail(LocalFile $file, $lines, array $options = [])
87
    {
88
        $this->options = $options;
89
        $this->lines = $lines;
90
91
        $postfix = $this->getOption('postfix', 'tail');
92
        $output = $this->getTargetFile($file, $postfix);
93
94
        $this->log(LogLevel::INFO, "Retrieving the last {lines} from file {file}", [
95
            'lines' => $this->lines,
96
            'file'  => $file,
97
        ]);
98
99
        $cmd = sprintf('tail -n %s %s > %s', $this->lines, $file->getPath(), $output->getPath());
100
101
        return $this->processFile($file, $output, $cmd, $this->getOption('keepOldFile', true));
102
    }
103
}
104