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 ( f4b9f8...380f59 )
by James
45:22 queued 31:24
created

UpdateController::updateCheck()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 39
rs 8.9297
c 0
b 0
f 0
cc 6
nc 16
nop 0
1
<?php
2
/**
3
 * UpdateController.php
4
 * Copyright (c) 2019 [email protected]
5
 *
6
 * This file is part of Firefly III (https://github.com/firefly-iii).
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program 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 Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
/** @noinspection PhpMethodParametersCountMismatchInspection */
22
declare(strict_types=1);
23
24
namespace FireflyIII\Http\Controllers\Admin;
25
26
use FireflyIII\Exceptions\FireflyException;
27
use FireflyIII\Helpers\Update\UpdateTrait;
28
use FireflyIII\Http\Controllers\Controller;
29
use FireflyIII\Http\Middleware\IsDemoUser;
30
use FireflyIII\Http\Middleware\IsSandStormUser;
31
use Illuminate\Http\JsonResponse;
32
use Illuminate\Http\Request;
33
use Log;
34
35
/**
36
 * Class HomeController.
37
 */
38
class UpdateController extends Controller
39
{
40
    use UpdateTrait;
0 ignored issues
show
Bug introduced by
The trait FireflyIII\Helpers\Update\UpdateTrait requires the property $data which is not provided by FireflyIII\Http\Controllers\Admin\UpdateController.
Loading history...
41
42
    /**
43
     * ConfigurationController constructor.
44
     */
45
    public function __construct()
46
    {
47
        parent::__construct();
48
        $this->middleware(
49
            static function ($request, $next) {
50
                app('view')->share('title', (string)trans('firefly.administration'));
51
                app('view')->share('mainTitleIcon', 'fa-hand-spock-o');
52
53
                return $next($request);
54
            }
55
        );
56
        $this->middleware(IsDemoUser::class)->except(['index']);
57
        $this->middleware(IsSandStormUser::class)->except(['index']);
58
    }
59
60
    /**
61
     * Show page with update options.
62
     *
63
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
64
     * @throws \Psr\Container\NotFoundExceptionInterface
65
     * @throws \Psr\Container\ContainerExceptionInterface
66
     */
67
    public function index()
68
    {
69
        $subTitle        = (string)trans('firefly.update_check_title');
70
        $subTitleIcon    = 'fa-star';
71
        $permission      = app('fireflyconfig')->get('permission_update_check', -1);
72
        $channel         = app('fireflyconfig')->get('update_channel', 'stable');
73
        $selected        = $permission->data;
74
        $channelSelected = $channel->data;
75
        $options         = [
76
            -1 => (string)trans('firefly.updates_ask_me_later'),
77
            0  => (string)trans('firefly.updates_do_not_check'),
78
            1  => (string)trans('firefly.updates_enable_check'),
79
        ];
80
81
        $channelOptions = [
82
            'stable' => (string)trans('firefly.update_channel_stable'),
83
            'beta'   => (string)trans('firefly.update_channel_beta'),
84
            'alpha'  => (string)trans('firefly.update_channel_alpha'),
85
        ];
86
87
        return view('admin.update.index', compact('subTitle', 'subTitleIcon', 'selected', 'options', 'channelSelected', 'channelOptions'));
88
    }
89
90
    /**
91
     * Post new settings.
92
     *
93
     * @param Request $request
94
     *
95
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
96
     */
97
    public function post(Request $request)
98
    {
99
        $checkForUpdates = (int)$request->get('check_for_updates');
100
        $channel         = $request->get('update_channel');
101
        $channel         = in_array($channel, ['stable', 'beta', 'alpha'], true) ? $channel : 'stable';
102
        app('fireflyconfig')->set('permission_update_check', $checkForUpdates);
103
        app('fireflyconfig')->set('last_update_check', time());
104
        app('fireflyconfig')->set('update_channel', $channel);
105
        session()->flash('success', (string)trans('firefly.configuration_updated'));
106
107
        return redirect(route('admin.update-check'));
108
    }
109
110
    /**
111
     * Does a manual update check.
112
     */
113
    public function updateCheck(): JsonResponse
114
    {
115
        $success       = true;
116
        $latestRelease = '1.0';
0 ignored issues
show
Unused Code introduced by
The assignment to $latestRelease is dead and can be removed.
Loading history...
117
        $resultString  = '';
118
        $versionCheck  = -2;
119
        $channel       = app('fireflyconfig')->get('update_channel', 'stable')->data;
120
121
        try {
122
            $latestRelease = $this->getLatestRelease();
123
        } catch (FireflyException $e) {
124
            Log::error($e->getMessage());
125
            $success = false;
126
        }
127
128
        // if error, tell the user.
129
        if (false === $success) {
130
            $resultString = (string)trans('firefly.update_check_error');
131
            session()->flash('error', $resultString);
132
        }
133
134
        // if not, compare and tell the user.
135
        if (true === $success) {
136
            $versionCheck = $this->versionCheck($latestRelease);
137
            $resultString = $this->parseResult($versionCheck, $latestRelease);
138
        }
139
140
        Log::debug(sprintf('Result string is: "%s"', $resultString));
141
142
        if (0 !== $versionCheck && '' !== $resultString) {
143
            // flash info
144
            session()->flash('info', $resultString);
145
        }
146
        app('fireflyconfig')->set('last_update_check', time());
147
148
        return response()->json(
149
            [
150
                'result'  => $resultString,
151
                'channel' => $channel,
152
            ]
153
        );
154
    }
155
}
156