Passed
Push — master ( 554f4f...40d218 )
by James
03:44 queued 16s
created

StartImport::startImport()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 31
rs 9.584
cc 3
nc 3
nop 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * StartImport.php
5
 * Copyright (c) 2020 [email protected]
6
 *
7
 * This file is part of the Firefly III CSV importer
8
 * (https://github.com/firefly-iii/csv-importer).
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22
 */
23
24
namespace App\Console;
25
26
use App\Exceptions\ImportException;
27
use App\Services\CSV\Configuration\Configuration;
28
use App\Services\CSV\File\FileReader;
29
use App\Services\Import\ImportRoutineManager;
30
use Log;
31
/**
32
 * Trait StartImport
33
 */
34
trait StartImport
35
{
36
    use ManageMessages;
37
    /**
38
     * @param string $csv
39
     * @param array  $configuration
40
     *
41
     * @return int
42
     */
43
    private function startImport(string $csv, array $configuration): int
44
    {
45
        Log::debug(sprintf('Now in %s', __METHOD__));
46
        $configObject = Configuration::fromFile($configuration);
47
        $manager      = new ImportRoutineManager;
48
49
        try {
50
            $manager->setConfiguration($configObject);
51
        } catch (ImportException $e) {
52
            $this->error($e->getMessage());
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
            $this->/** @scrutinizer ignore-call */ 
53
                   error($e->getMessage());
Loading history...
53
54
            return 1;
55
        }
56
        $manager->setReader(FileReader::getReaderFromContent($csv));
57
        try {
58
            $manager->start();
59
        } catch (ImportException $e) {
60
            $this->error($e->getMessage());
61
62
            return 1;
63
        }
64
65
        $messages = $manager->getAllMessages();
66
        $warnings = $manager->getAllWarnings();
67
        $errors   = $manager->getAllErrors();
68
69
        $this->listMessages('ERROR', $errors);
70
        $this->listMessages('Warning', $warnings);
71
        $this->listMessages('Message', $messages);
72
73
        return 0;
74
    }
75
}
76