Completed
Pull Request — develop (#19)
by
unknown
05:29 queued 02:09
created

ImportCommandAbstract::execute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0052

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 32
ccs 18
cts 19
cp 0.9474
rs 8.439
cc 6
eloc 17
nc 6
nop 2
crap 6.0052
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
use Symfony\Component\Finder\SplFileInfo;
15
16
/**
17
 * @author   List of contributors <https://github.com/libgraviton/import-export/graphs/contributors>
18
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
19
 * @link     http://swisscom.ch
20
 */
21
abstract class ImportCommandAbstract extends Command
22
{
23
    /**
24
     * @var Finder
25
     */
26
    protected $finder;
27
28
    /**
29
     * @param Finder $finder finder instance
30
     */
31 5
    public function __construct(
32
        Finder $finder
33
    ) {
34 5
        $this->finder = $finder;
35 5
        parent::__construct();
36 5
    }
37
38
    /**
39
     * Executes the current command.
40
     *
41
     * @param InputInterface  $input  User input on console
42
     * @param OutputInterface $output Output of the command
43
     *
44
     * @return void
45
     */
46 5
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48 5
        $files = $input->getArgument('file');
49 5
        $finder = $this->finder->files();
50
51
        /**
52
         * @param SplFileInfo $file
53
         * @return bool
54
         */
55 5
        $filter = function (SplFileInfo $file) {
56 5
            if (!in_array($file->getExtension(), ['yml','json']) || $file->isDir()) {
57
                return false;
58
            }
59 5
            return true;
60 5
        };
61
62 5
        foreach ($files as $file) {
63 5
            if (is_file($file)) {
64 4
                $finder->in(dirname($file))->name(basename($file));
65 4
            } else {
66 1
                $finder->in($file);
67
            }
68 5
        }
69
70 5
        $finder->ignoreDotFiles(true)->filter($filter);
71
        
72
        try {
73 5
            $this->doImport($finder, $input, $output);
74 5
        } catch (MissingTargetException $e) {
75 1
            $output->writeln('<error>' . $e->getMessage() . '</error>');
76
        }
77 4
    }
78
79
    /**
80
     * the actual command can do his import here..
81
     *
82
     * @param Finder          $finder finder
83
     * @param InputInterface  $input  input
84
     * @param OutputInterface $output output
85
     *
86
     * @return mixed
87
     */
88
    abstract protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output);
89
}
90