Completed
Push — develop ( 9af4bd...daa113 )
by
unknown
7s
created

ImportCommandAbstract::doImport()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
/**
3
 * base abstract for import based commands where a bunch of file must be collected and
4
 * done something with them..
5
 */
6
7
namespace Graviton\ImportExport\Command;
8
9
use Graviton\ImportExport\Exception\MissingTargetException;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Finder\Finder;
14
15
/**
16
 * @author   List of contributors <https://github.com/libgraviton/import-export/graphs/contributors>
17
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
18
 * @link     http://swisscom.ch
19
 */
20
abstract class ImportCommandAbstract extends Command
21
{
22
    /**
23
     * @var Finder
24
     */
25
    protected $finder;
26
27
    /**
28
     * @param Finder $finder finder instance
29
     */
30 5
    public function __construct(
31
        Finder $finder
32
    ) {
33 5
        $this->finder = $finder;
34 5
        parent::__construct();
35 5
    }
36
37
    /**
38
     * Executes the current command.
39
     *
40
     * @param InputInterface  $input  User input on console
41
     * @param OutputInterface $output Output of the command
42
     *
43
     * @return void
44
     */
45 5
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47 5
        $files = $input->getArgument('file');
48 5
        $finder = $this->finder->files();
49
50 5
        foreach ($files as $file) {
51 5
            if (is_file($file)) {
52 4
                $finder = $finder->in(dirname($file))->name(basename($file));
53 4
            } else {
54 1
                $finder = $finder->in($file);
55
            }
56 5
        }
57
58
        try {
59 5
            $this->doImport($finder, $input, $output);
60 5
        } catch (MissingTargetException $e) {
61 1
            $output->writeln('<error>' . $e->getMessage() . '</error>');
62
        }
63 5
    }
64
65
    /**
66
     * the actual command can do his import here..
67
     *
68
     * @param Finder          $finder finder
69
     * @param InputInterface  $input  input
70
     * @param OutputInterface $output output
71
     *
72
     * @return mixed
73
     */
74
    abstract protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output);
75
}
76