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 ( 59d732...3562ec )
by James
28:30 queued 12:37
created

SpectreConfigurator::getNextData()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 23
nc 3
nop 0
1
<?php
2
/**
3
 * SpectreConfigurator.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\Import\Configuration;
24
25
use FireflyIII\Exceptions\FireflyException;
26
use FireflyIII\Models\ImportJob;
27
use FireflyIII\Support\Import\Configuration\Spectre\HaveAccounts;
28
use Log;
29
30
/**
31
 * Class SpectreConfigurator.
32
 */
33
class SpectreConfigurator implements ConfiguratorInterface
34
{
35
    /** @var ImportJob */
36
    private $job;
37
38
    /** @var string */
39
    private $warning = '';
40
41
    /**
42
     * ConfiguratorInterface constructor.
43
     */
44
    public function __construct()
45
    {
46
    }
47
48
    /**
49
     * Store any data from the $data array into the job.
50
     *
51
     * @param array $data
52
     *
53
     * @return bool
54
     */
55
    public function configureJob(array $data): bool
56
    {
57
        $config = $this->job->configuration;
58
        $stage  = $config['stage'];
59
        $status = $this->job->status;
0 ignored issues
show
Unused Code introduced by
$status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
        Log::debug(sprintf('in getNextData(), for stage "%s".', $stage));
61
        switch ($stage) {
62
            case 'have-accounts':
63
                /** @var HaveAccounts $class */
64
                $class = app(HaveAccounts::class);
65
                $class->setJob($this->job);
66
                $class->storeConfiguration($data);
67
68
                // update job for next step and set to "configured".
69
                $config                   = $this->job->configuration;
70
                $config['stage']          = 'have-account-mapping';
71
                $this->job->configuration = $config;
72
                $this->job->status        = 'configured';
73
                $this->job->save();
74
                return true;
75
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
76
            default:
77
                throw new FireflyException(sprintf('Cannot store configuration when job is in state "%s"', $stage));
78
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
79
        }
80
    }
81
82
    /**
83
     * Return the data required for the next step in the job configuration.
84
     *
85
     * @return array
86
     */
87
    public function getNextData(): array
88
    {
89
        $config = $this->job->configuration;
90
        $stage  = $config['stage'];
91
        $status = $this->job->status;
0 ignored issues
show
Unused Code introduced by
$status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
92
        Log::debug(sprintf('in getNextData(), for stage "%s".', $stage));
93
        switch ($stage) {
94
            case 'has-token':
95
                // simply redirect to Spectre.
96
                $config['is-redirected'] = true;
97
                $config['stage']         = 'user-logged-in';
98
                $status                  = 'configured';
99
                break;
100
            case 'have-accounts':
101
                // use special class:
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
                /** @var HaveAccounts $class */
103
                $class = app(HaveAccounts::class);
104
                $class->setJob($this->job);
105
                $data = $class->getData();
106
107
                return $data;
108
            default:
109
                return [];
110
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
111
112
        }
113
114
        // update config and status:
115
        $this->job->configuration = $config;
116
        $this->job->status        = $status;
117
        $this->job->save();
118
119
        return $this->job->configuration;
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getNextView(): string
126
    {
127
        $config = $this->job->configuration;
128
        $stage  = $config['stage'];
129
        Log::debug(sprintf('in getNextView(), for stage "%s".', $stage));
130
        switch ($stage) {
131
            case 'has-token':
132
                // redirect to Spectre.
133
                return 'import.spectre.redirect';
134
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
135
            case 'have-accounts':
136
                return 'import.spectre.accounts';
137
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
138
            default:
139
                return '';
140
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
141
142
        }
143
    }
144
145
    /**
146
     * Return possible warning to user.
147
     *
148
     * @return string
149
     */
150
    public function getWarningMessage(): string
151
    {
152
        return $this->warning;
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    public function isJobConfigured(): bool
159
    {
160
        $config = $this->job->configuration;
161
        $stage  = $config['stage'];
162
        Log::debug(sprintf('in isJobConfigured(), for stage "%s".', $stage));
163
        switch ($stage) {
164
            case 'has-token':
165
            case 'have-accounts':
166
                Log::debug('isJobConfigured returns false');
167
168
                return false;
169
            default:
170
                Log::debug('isJobConfigured returns true');
171
172
                return true;
173
        }
174
    }
175
176
    /**
177
     * @param ImportJob $job
178
     */
179
    public function setJob(ImportJob $job)
180
    {
181
        $defaultConfig           = [
182
            'has-token'       => false,
183
            'token'           => '',
184
            'token-expires'   => 0,
185
            'token-url'       => '',
186
            'is-redirected'   => false,
187
            'customer'        => null,
188
            'login'           => null,
189
            'stage'           => 'initial',
190
            'accounts'        => '',
191
            'accounts-mapped' => '',
192
            'auto-start'      => true,
193
        ];
194
        $extendedStatus          = $job->extended_status;
195
        $extendedStatus['steps'] = 100;
196
197
198
        $config               = $job->configuration;
199
        $finalConfig          = array_merge($defaultConfig, $config);
200
        $job->configuration   = $finalConfig;
201
        $job->extended_status = $extendedStatus;
202
        $job->save();
203
        $this->job = $job;
204
    }
205
}
206