Completed
Push — develop ( 43816c...8775b9 )
by Narcotic
10s
created

ImportCommandAbstract::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 2
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 6
    public function __construct(
32
        Finder $finder
33
    ) {
34 6
        $this->finder = $finder;
35 6
        parent::__construct();
36 6
    }
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 integer
45
     */
46 6
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48 6
        $exitCode = 1;
49
        try {
50 6
            $exitCode = $this->doImport($this->getFinder($input), $input, $output);
0 ignored issues
show
Bug introduced by
It seems like $this->getFinder($input) targeting Graviton\ImportExport\Co...ndAbstract::getFinder() can also be of type array<integer,object<Sym...nt\Finder\SplFileInfo>>; however, Graviton\ImportExport\Co...andAbstract::doImport() does only seem to accept object<Symfony\Component\Finder\Finder>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
51 6
        } catch (MissingTargetException $e) {
52 1
            $output->writeln('<error>' . $e->getMessage() . '</error>');
53
        }
54 6
        return $exitCode;
55
    }
56
57
    /**
58
     * Returns a Finder according to the input params
59
     *
60
     * @param InputInterface $input User input on console
61
     *
62
     * @return Finder|SplFileInfo[] finder
63
     */
64 6
    protected function getFinder($input)
65
    {
66 6
        $files = $input->getArgument('file');
67 6
        $inputFile = $input->getOption('input-file');
68
69 6
        if (empty($files) && is_null($inputFile)) {
70
            throw new \LogicException('You either need to provide <file> arguments or the --input-file option.');
71
        }
72
73 6
        $finder = $this->finder->files();
74
75
        /**
76
         * @param SplFileInfo $file
77
         * @return bool
78
         */
79 View Code Duplication
        $filter = function (SplFileInfo $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
80 6
            if (!in_array($file->getExtension(), ['yml','json']) || $file->isDir()) {
81 1
                return false;
82
            }
83 6
            return true;
84 6
        };
85
86 6
        if (is_null($inputFile)) {
87
            // normal way - file arguments..
88 6 View Code Duplication
            foreach ($files as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
89 6
                if (is_file($file)) {
90 4
                    $finder->in(dirname($file))->name(basename($file));
91 4
                } else {
92 2
                    $finder->in($file);
93
                }
94 6
            }
95 6
        } else {
96
            // file list via input file
97
            if (!file_exists($inputFile)) {
98
                throw new \LogicException('File '.$inputFile.' does not seem to exist.');
99
            }
100
101
            $fileList = explode(PHP_EOL, file_get_contents($inputFile));
102
103
            $fileList = array_filter(
104
                $fileList,
105
                function ($val) {
106
                    if (!empty($val)) {
107
                        return true;
108
                    }
109
                    return false;
110
                }
111
            );
112
113
            $finder->append($fileList);
114
        }
115
116 6
        $finder->ignoreDotFiles(true)->filter($filter);
117
118 6
        return $finder;
119
    }
120
121
    /**
122
     * the actual command can do his import here..
123
     *
124
     * @param Finder          $finder finder
125
     * @param InputInterface  $input  input
126
     * @param OutputInterface $output output
127
     *
128
     * @return mixed
129
     */
130
    abstract protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output);
131
}
132