Completed
Push — master ( 794f3d...1e9726 )
by diego
07:00
created

CreateCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
namespace Diego\Grepper\Command;
3
4
use Diego\Grepper\File;
5
use Diego\Grepper\FileNotFound;
6
use Symfony\Component\Console\Helper\ProgressBar;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Class CreateCommand
14
 */
15
class CreateCommand extends AbstractInputFileBasedCommand
16
{
17
    protected function configure()
18
    {
19
        $this->setName('create')
20
            ->setDescription('Grep lines from one file and write them to another')
21
            ->addArgument('input', InputArgument::REQUIRED)
22
            ->addArgument('expression', InputArgument::REQUIRED)
23
            ->addArgument('output', InputArgument::REQUIRED)
24
            ->addOption('progress', 'p', InputOption::VALUE_NONE, 'You should only use the progress if every single expression takes some time');
25
    }
26
27
    /**
28
     * @param InputInterface $input
29
     * @param OutputInterface $output
30
     * @return int
31
     */
32
    public function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        $this->assureInputFileReadable($input);
35
        $validLines = new \RegexIterator(new \SplFileObject($input->getArgument('input')), $input->getArgument('expression'));
36
37
        $target = new \SplFileObject($input->getArgument('output'), 'w');
38
        foreach ($validLines as $line) {
39
            $target->fwrite($line);
40
        }
41
42
        return 0;
43
    }
44
}
45