RawImageCacheService::run()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 41
c 0
b 0
f 0
ccs 0
cts 29
cp 0
rs 8.439
cc 6
eloc 24
nc 6
nop 2
crap 42
1
<?php namespace Onigoetz\Imagecache\Support\Raw;
2
3
use Onigoetz\Imagecache\Exceptions\InvalidPresetException;
4
use Onigoetz\Imagecache\Exceptions\NotFoundException;
5
use Onigoetz\Imagecache\Manager;
6
use Onigoetz\Imagecache\Transfer;
7
8
class RawImageCacheService
9
{
10
    public static function run($config, $request)
11
    {
12
        $imagecache = new Manager($config);
13
14
        try {
15
            $final_file = $imagecache->handleRequest($request['preset'], $request['file']);
16
        } catch (InvalidPresetException $e) {
17
            header('HTTP/1.0 404 Not Found');
18
            echo $e->getMessage();
19
20
            return;
21
        } catch (NotFoundException $e) {
22
            header('HTTP/1.0 404 Not Found');
23
            echo $e->getMessage();
24
25
            return;
26
        } catch (\RuntimeException $e) {
27
            header('HTTP/1.0 500 Internal Server Error');
28
            echo $e->getMessage();
29
30
            return;
31
        }
32
33
        $transfer = new Transfer($final_file);
34
35
        // if the status is 304, we don't
36
        // need to send the content with it
37
        if ($transfer->getStatus() == 304) {
38
            header('HTTP/1.1 304 Not Modified');
39
40
            return;
41
        }
42
43
        header('HTTP/1.1 200 OK');
44
45
        foreach ($transfer->getFormattedHeaders() as $header) {
46
            header($header);
47
        }
48
49
        $transfer->stream();
50
    }
51
}
52