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.

SvgFixerMiddleware   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleSVGFile() 0 15 2
A handle() 0 15 6
1
<?php
2
3
namespace DigiFactory\SvgFixer;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\UploadedFile;
8
use Illuminate\Support\Str;
9
10
class SvgFixerMiddleware
11
{
12
    protected $response;
13
    protected $xmlDeclaration = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
14
15
    public function handle(Request $request, Closure $next)
16
    {
17
        // Check if request is a POST request and has at least one file
18
        if ($request->method() === 'POST' && $request->files->count() > 0) {
19
            /** @var UploadedFile $file */
20
            foreach ($request->files as $file) {
21
                // Check if uploaded file is an SVG
22
                if ($file instanceof UploadedFile && Str::startsWith($file->getMimeType(), 'image/svg')) {
23
                    $this->handleSVGFile($file);
24
                }
25
            }
26
        }
27
28
        return $next($request);
29
    }
30
31
    private function handleSVGFile($file)
32
    {
33
        $handle = fopen($file->getPathname(), 'r+');
34
        $contents = fread($handle, filesize($file->getPathname()));
35
36
        rewind($handle);
37
38
        // If the file starts with <svg the XML declaration is missing
39
        if (Str::startsWith($contents, '<svg')) {
40
            // Add XML declaration
41
            fwrite($handle, $this->xmlDeclaration.PHP_EOL.$contents);
42
        }
43
44
        fclose($handle);
45
    }
46
}
47