GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f7eb78...e47b21 )
by James
13:02 queued 08:20
created

Initial::storeConfiguration()   F

Complexity

Conditions 10
Paths 512

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 3.2187
c 0
b 0
f 0
cc 10
eloc 23
nc 512
nop 1

How to fix   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
 * Initial.php
4
 * Copyright (c) 2017 [email protected]
5
 *
6
 * This file is part of Firefly III.
7
 *
8
 * Firefly III is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * Firefly III is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with Firefly III.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
declare(strict_types=1);
22
23
namespace FireflyIII\Support\Import\Configuration\Csv;
24
25
use ExpandedForm;
26
use FireflyIII\Models\AccountType;
27
use FireflyIII\Models\ImportJob;
28
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
29
use FireflyIII\Support\Import\Configuration\ConfigurationInterface;
30
use Log;
31
32
/**
33
 * Class CsvInitial.
34
 */
35
class Initial implements ConfigurationInterface
36
{
37
    private $job;
38
39
    /**
40
     * @return array
41
     */
42
    public function getData(): array
43
    {
44
        /** @var AccountRepositoryInterface $accountRepository */
45
        $accountRepository = app(AccountRepositoryInterface::class);
46
        $accounts          = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
47
        $delimiters        = [
48
            ','   => trans('form.csv_comma'),
49
            ';'   => trans('form.csv_semicolon'),
50
            'tab' => trans('form.csv_tab'),
51
        ];
52
53
        $specifics = [];
54
55
        // collect specifics.
56
        foreach (config('csv.import_specifics') as $name => $className) {
57
            $specifics[$name] = [
58
                'name'        => $className::getName(),
59
                'description' => $className::getDescription(),
60
            ];
61
        }
62
63
        $data = [
64
            'accounts'   => ExpandedForm::makeSelectList($accounts),
65
            'specifix'   => [],
66
            'delimiters' => $delimiters,
67
            'specifics'  => $specifics,
68
        ];
69
70
        return $data;
71
    }
72
73
    /**
74
     * Return possible warning to user.
75
     *
76
     * @return string
77
     */
78
    public function getWarningMessage(): string
79
    {
80
        return '';
81
    }
82
83
    /**
84
     * @param ImportJob $job
85
     *
86
     * @return ConfigurationInterface
87
     */
88
    public function setJob(ImportJob $job): ConfigurationInterface
89
    {
90
        $this->job = $job;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Store the result.
97
     *
98
     * @param array $data
99
     *
100
     * @return bool
101
     */
102
    public function storeConfiguration(array $data): bool
103
    {
104
        /** @var AccountRepositoryInterface $repository */
105
        $repository = app(AccountRepositoryInterface::class);
106
        $importId   = $data['csv_import_account'] ?? 0;
107
        $account    = $repository->find(intval($importId));
108
109
        $hasHeaders                        = isset($data['has_headers']) && 1 === intval($data['has_headers']) ? true : false;
110
        $config                            = $this->job->configuration;
111
        $config['initial-config-complete'] = true;
112
        $config['has-headers']             = $hasHeaders;
113
        $config['date-format']             = $data['date_format'];
114
        $config['delimiter']               = $data['csv_delimiter'];
115
        $config['delimiter']               = 'tab' === $config['delimiter'] ? "\t" : $config['delimiter'];
116
        $config['apply_rules']             = isset($data['apply_rules']) && 1 === intval($data['apply_rules']) ? true : false;
117
        $config['match_bills']             = isset($data['match_bills']) && 1 === intval($data['match_bills']) ? true : false;
118
119
        Log::debug('Entered import account.', ['id' => $importId]);
120
121
        if (null !== $account->id) {
122
            Log::debug('Found account.', ['id' => $account->id, 'name' => $account->name]);
123
            $config['import-account'] = $account->id;
124
        }
125
126
        if (null === $account->id) {
127
            Log::error('Could not find anything for csv_import_account.', ['id' => $importId]);
128
        }
129
130
        $config                   = $this->storeSpecifics($data, $config);
131
        $this->job->configuration = $config;
132
        $this->job->save();
133
134
        return true;
135
    }
136
137
    /**
138
     * @param array $data
139
     * @param array $config
140
     *
141
     * @return array
142
     */
143
    private function storeSpecifics(array $data, array $config): array
144
    {
145
        // loop specifics.
146
        if (isset($data['specifics']) && is_array($data['specifics'])) {
147
            $names = array_keys($data['specifics']);
148
            foreach ($names as $name) {
149
                // verify their content.
150
                $className = sprintf('FireflyIII\Import\Specifics\%s', $name);
151
                if (class_exists($className)) {
152
                    $config['specifics'][$name] = 1;
153
                }
154
            }
155
        }
156
157
        return $config;
158
    }
159
}
160