PHPStreamer::stream()   A
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 16
c 4
b 1
f 0
dl 0
loc 20
ccs 0
cts 16
cp 0
rs 9.1111
cc 6
nc 16
nop 0
crap 42
1
<?php
2
3
namespace App\Services\Streamers;
4
5
use DaveRandom\Resume\FileResource;
6
use function DaveRandom\Resume\get_request_header;
7
use DaveRandom\Resume\InvalidRangeHeaderException;
8
use DaveRandom\Resume\NonExistentFileException;
9
use DaveRandom\Resume\RangeSet;
10
use DaveRandom\Resume\ResourceServlet;
11
use DaveRandom\Resume\SendFileFailureException;
12
use DaveRandom\Resume\UnreadableFileException;
13
use DaveRandom\Resume\UnsatisfiableRangeException;
14
use Symfony\Component\HttpFoundation\Response;
15
16
class PHPStreamer extends Streamer implements DirectStreamerInterface
17
{
18
    public function stream()
19
    {
20
        try {
21
            $rangeSet = RangeSet::createFromHeader(get_request_header('Range'));
22
            $resource = new FileResource($this->song->path);
23
            (new ResourceServlet($resource))->sendResource($rangeSet);
24
        } catch (InvalidRangeHeaderException $e) {
25
            abort(Response::HTTP_BAD_REQUEST);
26
        } catch (UnsatisfiableRangeException $e) {
27
            abort(Response::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE);
28
        } catch (NonExistentFileException $e) {
29
            abort(Response::HTTP_NOT_FOUND);
30
        } catch (UnreadableFileException $e) {
31
            abort(Response::HTTP_INTERNAL_SERVER_ERROR);
32
        } catch (SendFileFailureException $e) {
33
            abort_unless(headers_sent(), Response::HTTP_INTERNAL_SERVER_ERROR);
34
            echo "An error occurred while attempting to send the requested resource: {$e->getMessage()}";
35
        }
36
37
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
38
    }
39
}
40