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 ( 37b02e...ebbbe1 )
by James
08:59
created

app/Console/Commands/ScanAttachments.php (3 issues)

1
<?php
2
declare(strict_types=1);
3
/**
4
 * ScanAttachments.php
5
 * Copyright (c) 2017 [email protected]
6
 *
7
 * This file is part of Firefly III.
8
 *
9
 * Firefly III is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Firefly III 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 General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
namespace FireflyIII\Console\Commands;
24
25
use Crypt;
26
use FireflyIII\Models\Attachment;
27
use Illuminate\Console\Command;
28
use Illuminate\Contracts\Encryption\DecryptException;
29
use Illuminate\Contracts\Filesystem\FileNotFoundException;
30
use Storage;
31
32
/**
33
 * Class ScanAttachments.
34
 */
35
class ScanAttachments extends Command
36
{
37
    /**
38
     * The console command description.
39
     *
40
     * @var string
41
     */
42
    protected $description = 'Rescan all attachments and re-set the MD5 hash and mime.';
43
44
    /**
45
     * The name and signature of the console command.
46
     *
47
     * @var string
48
     */
49
    protected $signature = 'firefly:scan-attachments';
50
51
    /**
52
     * Execute the console command.
53
     */
54
    public function handle()
55
    {
56
        $attachments = Attachment::get();
57
        $disk        = Storage::disk('upload');
0 ignored issues
show
Bug Best Practice introduced by
The method Illuminate\Support\Facades\Storage::disk() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        /** @scrutinizer ignore-call */ 
58
        $disk        = Storage::disk('upload');
Loading history...
58
        /** @var Attachment $attachment */
59
        foreach ($attachments as $attachment) {
60
            $fileName = $attachment->fileName();
61
            try {
62
                $content = $disk->get($fileName);
63
            } catch (FileNotFoundException $e) {
64
                $this->error(sprintf('Could not find data for attachment #%d', $attachment->id));
65
                continue;
66
            }
67
            try {
68
                $decrypted = Crypt::decrypt($content);
69
            } catch (DecryptException $e) {
70
                $this->error(sprintf('Could not decrypt data of attachment #%d', $attachment->id));
71
                continue;
72
            }
73
            $tmpfname = tempnam(sys_get_temp_dir(), 'FireflyIII');
74
            file_put_contents($tmpfname, $decrypted);
75
            $md5              = md5_file($tmpfname);
76
            $mime             = mime_content_type($tmpfname);
77
            $attachment->md5  = $md5;
0 ignored issues
show
The property md5 does not seem to exist on FireflyIII\Models\Attachment. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
78
            $attachment->mime = $mime;
0 ignored issues
show
The property mime does not seem to exist on FireflyIII\Models\Attachment. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
79
            $attachment->save();
80
            $this->line(sprintf('Fixed attachment #%d', $attachment->id));
81
        }
82
    }
83
}
84