StartSync::startSync()   C
last analyzed

Complexity

Conditions 12
Paths 10

Size

Total Lines 67
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 67
rs 6.9666
cc 12
nc 10
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * StartSync.php
4
 * Copyright (c) 2020 [email protected].
5
 *
6
 * This file is part of the Firefly III bunq importer
7
 * (https://github.com/firefly-iii/bunq-importer).
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace App\Console;
26
27
use App\Exceptions\ImportException;
28
use App\Services\Configuration\Configuration;
29
use App\Services\Sync\RoutineManager as SyncRoutineManager;
30
31
/**
32
 * Trait StartSync.
33
 */
34
trait StartSync
35
{
36
    /**
37
     * @param array $configuration
38
     *
39
     * @return int
40
     */
41
    private function startSync(array $configuration): int
42
    {
43
        app('log')->debug(sprintf('Now in %s', __METHOD__));
44
        $configObject = Configuration::fromFile($configuration);
45
46
        // first download from bunq
47
        $manager = new SyncRoutineManager;
48
        $manager->setDownloadIdentifier($this->downloadIdentifier);
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
        try {
57
            $manager->start();
58
        } catch (ImportException $e) {
59
            $this->error($e->getMessage());
60
61
            return 1;
62
        }
63
64
        $messages = $manager->getAllMessages();
65
        $warnings = $manager->getAllWarnings();
66
        $errors   = $manager->getAllErrors();
67
68
        if (count($errors) > 0) {
69
            /**
70
             * @var int   $index
71
             * @var array $error
72
             */
73
            foreach ($errors as $index => $error) {
74
                /** @var string $line */
75
                foreach ($error as $line) {
76
                    $this->error(sprintf('ERROR in line     #%d: %s', $index + 1, $line));
77
                }
78
            }
79
        }
80
81
        if (count($warnings) > 0) {
82
            /**
83
             * @var int   $index
84
             * @var array $warning
85
             */
86
            foreach ($warnings as $index => $warning) {
87
                /** @var string $line */
88
                foreach ($warning as $line) {
89
                    $this->warn(sprintf('Warning from line #%d: %s', $index + 1, $line));
0 ignored issues
show
Bug introduced by
It seems like warn() 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

89
                    $this->/** @scrutinizer ignore-call */ 
90
                           warn(sprintf('Warning from line #%d: %s', $index + 1, $line));
Loading history...
90
                }
91
            }
92
        }
93
94
        if (count($messages) > 0) {
95
            /**
96
             * @var int   $index
97
             * @var array $message
98
             */
99
            foreach ($messages as $index => $message) {
100
                /** @var string $line */
101
                foreach ($message as $line) {
102
                    $this->info(sprintf('Message from line #%d: %s', $index + 1, strip_tags($line)));
0 ignored issues
show
Bug introduced by
It seems like info() 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

102
                    $this->/** @scrutinizer ignore-call */ 
103
                           info(sprintf('Message from line #%d: %s', $index + 1, strip_tags($line)));
Loading history...
103
                }
104
            }
105
        }
106
107
        return 0;
108
    }
109
}
110