Completed
Push — master ( 2d8f65...4181f5 )
by Stéphane
06:54
created

ImagecacheRegister3   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 45
c 0
b 0
f 0
ccs 0
cts 34
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B request() 0 29 5
A register() 0 12 1
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
13
class ImagecacheRegister3
14
{
15
    public function request()
16
    {
17
        return function (ServerRequestInterface $req, ResponseInterface $res, $args) {
18
19
20
            $preset = $args['preset'];
21
            $file = $args['file'];
22
23
            try {
24
                $final_file = $this->imagecache->handleRequest($preset, $file);
0 ignored issues
show
Bug introduced by
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...
25
            } catch (InvalidPresetException $e) {
26
                $res->getBody()->write($e->getMessage());
27
                return $res->withStatus(404);
28
            } catch (NotFoundException $e) {
29
                $res->getBody()->write($e->getMessage());
30
                return $res->withStatus(404);
31
            } catch (RuntimeException $e) {
32
                $res->getBody()->write($e->getMessage());
33
                return $res->withStatus(500);
34
            }
35
36
            $transfer = new Transfer($final_file);
37
            foreach ($transfer->getHeaders() as $key => $value) {
38
                $res = $res->withHeader($key, $value);
39
            }
40
41
            return $res->withStatus($transfer->getStatus())->withBody(new LazyOpenStream($final_file, 'r'));
42
        };
43
    }
44
45
    public static function register(App $app, $config)
46
    {
47
        $app->getContainer()['imagecache'] = function () use ($config) {
48
            return new Manager($config);
49
        };
50
51
        $app->get(
52
            "/{$config['path_web']}/{$config['path_cache']}/{preset}/{file:.*}",
53
            (new ImagecacheRegister)->request()
0 ignored issues
show
Bug introduced by
The method request() does not seem to exist on object<Onigoetz\Imagecac...lim\ImagecacheRegister>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
        )
55
            ->setName('onigoetz.imagecache');
56
    }
57
}
58