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

src/Support/Slim/ImagecacheRegister.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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