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 ( f38dc2...eafdef )
by Cees-Jan
12s
created

WebrootPreloadMiddleware::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 2
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use Narrowspark\Mimetypes\MimeTypeByExtensionGuesser;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Log\LoggerInterface;
8
use RingCentral\Psr7\Response;
9
use ScriptFUSION\Byte\ByteFormatter;
10
use function RingCentral\Psr7\stream_for;
11
12
final class WebrootPreloadMiddleware
13
{
14
    /**
15
     * @var array
16
     */
17
    private $files = [];
18
19 10
    public function __construct(string $webroot, LoggerInterface $logger = null)
20
    {
21 10
        $totalSize = 0;
22 10
        $byteFormatter = (new ByteFormatter())->setPrecision(2)->setFormat('%v%u');
23 10
        $directory = new \RecursiveDirectoryIterator($webroot);
24 10
        $directory = new \RecursiveIteratorIterator($directory);
25 10
        $directory = iterator_to_array($directory);
26 10
        usort($directory, function ($a, $b) {
27 10
            return $a->getPathname() <=> $b->getPathname();
28 10
        });
29 10
        foreach ($directory as $fileinfo) {
30 10
            if (!$fileinfo->isFile()) {
31 10
                continue;
32
            }
33
34 10
            $filePath = str_replace(
35
                [
36 10
                    $webroot,
37 10
                    DIRECTORY_SEPARATOR,
38 10
                    '//',
39
                ],
40
                [
41 10
                    DIRECTORY_SEPARATOR,
42 10
                    '/',
43 10
                    '/',
44
                ],
45 10
                $fileinfo->getPathname()
46
            );
47
48 10
            $this->files[$filePath] = [
49 10
                'contents' => file_get_contents($fileinfo->getPathname()),
50
            ];
51
52 10
            $mime = MimeTypeByExtensionGuesser::guess($fileinfo->getExtension());
53 10
            if (is_null($mime)) {
54 10
                $mime = 'application/octet-stream';
55
            }
56 10
            list($mime) = explode(';', $mime);
57 10
            if (strpos($mime, '/') !== false) {
58 10
                $this->files[$filePath]['mime'] = $mime;
59
            }
60
61 10
            if ($logger instanceof LoggerInterface) {
62 1
                $fileSize = strlen($this->files[$filePath]['contents']);
63 1
                $totalSize += $fileSize;
64 10
                $logger->debug($filePath . ': ' . $byteFormatter->format($fileSize));
65
            }
66
        }
67
68 10
        if ($logger instanceof LoggerInterface) {
69 1
            $logger->info('Preloaded ' . count($this->files) . ' file(s) with a combined size of ' . $byteFormatter->format($totalSize) . ' from "' . $webroot . '" into memory');
70
        }
71 10
    }
72
73 9
    public function __invoke(ServerRequestInterface $request, callable $next)
74
    {
75 9
        $path = $request->getUri()->getPath();
76 9
        if (!isset($this->files[$path])) {
77 1
            return $next($request);
78
        }
79
80 8
        $response = (new Response(200))->withBody(stream_for($this->files[$path]['contents']));
81 8
        if (!isset($this->files[$path]['mime'])) {
82
            return $response;
83
        }
84
85 8
        return $response->withHeader('Content-Type', $this->files[$path]['mime']);
86
    }
87
}
88