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.
Passed
Push — master ( 7d482a...c9f4cf )
by James
30:19 queued 11:35
created

NewFinTSJobHandler::validURI()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * NewFinTSJobHandler.php
4
 * Copyright (c) 2018 https://github.com/bnw
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
24
namespace FireflyIII\Support\Import\JobConfiguration\FinTS;
25
26
27
use FireflyIII\Import\JobConfiguration\FinTSConfigurationSteps;
28
use FireflyIII\Models\ImportJob;
29
use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface;
30
use FireflyIII\Support\FinTS\FinTS;
31
use Illuminate\Support\Facades\Crypt;
32
use Illuminate\Support\MessageBag;
33
34
/**
35
 *
36
 * Class NewFinTSJobHandler
37
 */
38
class NewFinTSJobHandler implements FinTSConfigurationInterface
39
{
40
    /** @var ImportJob */
41
    private $importJob;
42
    /** @var ImportJobRepositoryInterface */
43
    private $repository;
44
45
    /**
46
     * Store data associated with current stage.
47
     *
48
     * @param array $data
49
     *
50
     * @return MessageBag
51
     */
52
    public function configureJob(array $data): MessageBag
53
    {
54
        $config = [];
55
56
        $config['fints_url']       = trim($data['fints_url'] ?? '');
57
        $config['fints_port']      = (int)($data['fints_port'] ?? '');
58
        $config['fints_bank_code'] = (string)($data['fints_bank_code'] ?? '');
59
        $config['fints_username']  = (string)($data['fints_username'] ?? '');
60
        $config['fints_password']  = (string)(Crypt::encrypt($data['fints_password']) ?? '');
61
        $config['apply-rules']     = 1 === (int)$data['apply_rules'];
62
63
        // sanitize FinTS URL.
64
        $config['fints_url'] = $this->validURI($config['fints_url']) ? $config['fints_url'] : '';
65
66
        $this->repository->setConfiguration($this->importJob, $config);
67
68
69
        $incomplete = false;
70
        foreach ($config as $value) {
71
            $incomplete = '' === $value or $incomplete;
72
        }
73
74
        if ($incomplete) {
75
            return new MessageBag([trans('import.incomplete_fints_form')]);
76
        }
77
        $finTS = app(FinTS::class, ['config' => $this->importJob->configuration]);
78
        if (true !== ($checkConnection = $finTS->checkConnection())) {
79
            return new MessageBag([trans('import.fints_connection_failed', ['originalError' => $checkConnection])]);
80
        }
81
82
        $this->repository->setStage($this->importJob, FinTSConfigurationSteps::CHOOSE_ACCOUNT);
83
84
        return new MessageBag();
85
    }
86
87
    /**
88
     * Get the data necessary to show the configuration screen.
89
     *
90
     * @return array
91
     */
92
    public function getNextData(): array
93
    {
94
        $config = $this->importJob->configuration;
95
96
        return [
97
            'fints_url'       => $config['fints_url'] ?? '',
98
            'fints_port'      => $config['fints_port'] ?? '443',
99
            'fints_bank_code' => $config['fints_bank_code'] ?? '',
100
            'fints_username'  => $config['fints_username'] ?? '',
101
        ];
102
    }
103
104
    /**
105
     * @param ImportJob $importJob
106
     */
107
    public function setImportJob(ImportJob $importJob): void
108
    {
109
        $this->importJob  = $importJob;
110
        $this->repository = app(ImportJobRepositoryInterface::class);
111
        $this->repository->setUser($importJob->user);
112
    }
113
114
    /**
115
     * @param string $fints_url
116
     *
117
     * @return bool
118
     */
119
    private function validURI(string $fintsUri): bool
120
    {
121
        $res = filter_var($fintsUri, FILTER_VALIDATE_URL);
122
        if (false === $res) {
123
            return false;
124
        }
125
        $scheme = parse_url($fintsUri, PHP_URL_SCHEME);
126
127
        return 'https' === $scheme;
128
    }
129
130
131
}
132