Completed
Push — master ( 4181f5...2f550b )
by Stéphane
04:08
created

ImagecacheRegister::request()   B

Complexity

Conditions 5
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 28
Code Lines 19

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 21
cts 21
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 1
nop 0
crap 5
1
<?php namespace Onigoetz\Imagecache\Support\Slim;
2
3
use GuzzleHttp\Psr7\LazyOpenStream;
4
use Onigoetz\Imagecache\Exceptions\InvalidPresetException;
5
use Onigoetz\Imagecache\Exceptions\NotFoundException;
6
use Onigoetz\Imagecache\Manager;
7
use Onigoetz\Imagecache\Transfer;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use RuntimeException;
11
use Slim\App;
12 18
13
class ImagecacheRegister
14 18
{
15 18
    public function request()
16
    {
17 15
        return function (ServerRequestInterface $req, ResponseInterface $res, $args) {
18 6
19 12
            $preset = $args['preset'];
20
            $file = $args['file'];
21 18
22
            try {
23 18
                $final_file = $this->imagecache->handleRequest($preset, $file);
0 ignored issues
show
Bug introduced by Stéphane Goetz
The property imagecache does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24 12
            } catch (InvalidPresetException $e) {
25 14
                $res->getBody()->write($e->getMessage());
26
                return $res->withStatus(404);
27 12
            } catch (NotFoundException $e) {
28 11
                $res->getBody()->write($e->getMessage());
29 3
                return $res->withStatus(404);
30 3
            } catch (RuntimeException $e) {
31
                $res->getBody()->write($e->getMessage());
32 3
                return $res->withStatus(500);
33 6
            }
34 3
35 3
            $transfer = new Transfer($final_file);
36
            foreach ($transfer->getHeaders() as $key => $value) {
37 3
                $res = $res->withHeader($key, $value);
38 3
            }
39 3
40 3
            return $res->withStatus($transfer->getStatus())->withBody(new LazyOpenStream($final_file, 'r'));
41
        };
42 3
    }
43
44
    public static function register(App $app, $config)
45 3
    {
46 3
        $app->getContainer()['imagecache'] = function () use ($config) {
47 3
            return new Manager($config);
48
        };
49 3
50 8
        $app->get(
51 12
            "/{$config['path_web']}/{$config['path_cache']}/{preset}/{file:.*}",
52 18
            (new ImagecacheRegister)->request()
53 18
        )
54 18
            ->setName('onigoetz.imagecache');
55
    }
56
}
57