ImportCommand::checkParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
namespace QualityCode\TransformAndLoadBundle\Command;
4
5
use Symfony\Component\Console\Command\LockableTrait;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Filesystem\Filesystem;
11
12
/**
13
 * Import command line.
14
 */
15
class ImportCommand extends ContainerAwareCommand
16
{
17
    use LockableTrait;
18
19 1
    protected function configure()
20
    {
21
        $this
22 1
            ->setName('qltyc:import')
23 1
            ->setDescription('Import de données')
24 1
            ->addArgument('schema', InputArgument::REQUIRED, "Schema d'import")
25 1
            ->addArgument('file', InputArgument::REQUIRED, 'Fichier à importer')
26
        ;
27 1
    }
28
29
    /**
30
     * @param InputInterface  $input
31
     * @param OutputInterface $output
32
     *
33
     * @return int
34
     */
35 1
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 1
        if (0 < $this->checkParams($input, $output)) {
38 1
            return 0;
39
        }
40
41
        $schema = $input->getArgument('schema');
42
        $fileName = $input->getArgument('file');
43
44
        $schemaDefinition = $this->getContainer()->getParameter('qltyc_tl.imports.' . $schema);
45
46
        $begin = new \DateTime();
47
        $output->writeln('<comment>Début de l\'import du fichier ' . $fileName . ' avec le schema ' . $schema . ' à ' . $begin->format('d-m-Y G:i:s') . '</comment>');
48
49
        $this->getContainer()->get('qltyc.tl.import')->import($schemaDefinition, $fileName, $output);
50
51
        $end = new \DateTime();
52
        $output->writeln('');
53
        $output->writeln('<comment>L\'import du fichier ' . $fileName . ' utilisant le schema ' . $schema . ' est terminé à ' . $end->format('d-m-Y G:i:s') . '</comment>');
54
55
        $this->release();
56
    }
57
58
    /**
59
     * @param InputInterface  $input
60
     * @param OutputInterface $output
61
     *
62
     * @return int
63
     */
64 1
    private function checkParams(InputInterface $input, OutputInterface $output)
65
    {
66 1
        $errors = 0;
67
68 1
        $errors += $this->checkLock($output);
69 1
        $errors += $this->checkFile($input, $output);
70 1
        $errors += $this->checkSchema($input, $output);
71
72 1
        return $errors;
73
    }
74
75
    /**
76
     * @param OutputInterface $output
77
     *
78
     * @return int
79
     */
80 1
    private function checkLock(OutputInterface $output)
81
    {
82 1
        if (!$this->lock()) {
83
            $output->writeln('The command is already running in another process.');
84
85
            return 1;
86
        }
87
88 1
        return 0;
89
    }
90
91
    /**
92
     * @param InputInterface  $input
93
     * @param OutputInterface $output
94
     *
95
     * @return int
96
     */
97 1
    private function checkFile(InputInterface $input, OutputInterface $output)
98
    {
99 1
        $fileName = $input->getArgument('file');
100
101 1
        $fs = new Filesystem();
102
103 1
        if (!$fs->exists($fileName)) {
104 1
            $output->writeln('This file doesn\'t exist !');
105
106 1
            return 1;
107
        }
108
109
        return 0;
110
    }
111
112
    /**
113
     * @param InputInterface  $input
114
     * @param OutputInterface $output
115
     *
116
     * @return int
117
     */
118 1
    private function checkSchema(InputInterface $input, OutputInterface $output)
119
    {
120 1
        $schema = $input->getArgument('schema');
121 1
        if (!$this->getContainer()->hasParameter('qltyc_tl.imports.' . $schema)) {
122 1
            $output->writeln('This schema doesn\'t exist !');
123
124 1
            return 1;
125
        }
126
127
        return 0;
128
    }
129
}
130