Completed
Push — master ( 207930...8e22c1 )
by Stéphane
02:51
created

src/Support/Laravel/ImagecacheServiceProvider.php (1 issue)

Labels
Severity

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\Laravel;
2
3
use Illuminate\Support\ServiceProvider;
4
use Onigoetz\Imagecache\Exceptions\InvalidPresetException;
5
use Onigoetz\Imagecache\Exceptions\NotFoundException;
6
use Onigoetz\Imagecache\Manager;
7
use Onigoetz\Imagecache\Transfer;
8
9
class ImagecacheServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Add the namespace to config
13
     */
14 15
    public function registerConfiguration()
15
    {
16 15
        $this->publishes(
17
            [
18 15
                __DIR__ . '/../../config/imagecache.php' => config_path('imagecache.php'),
19
            ]
20 10
        );
21 15
    }
22
23 15
    public function getConfiguration()
24
    {
25 15
        return $this->app['config']->get('imagecache');
26
    }
27
28
    /**
29
     * Register imagecache
30
     */
31 15
    public function registerManager()
32
    {
33 15
        $this->app['imagecache'] = $this->app->share(
0 ignored issues
show
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

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...
34
            function () {
35 15
                $config = $this->getConfiguration();
36
37 15
                return new Manager($config);
38 5
            }
39 10
        );
40 15
    }
41
42 15
    public function registerRoute()
43
    {
44 15
        $config = $this->getConfiguration();
45
46 15
        $url = "{$config['path_web']}/{$config['path_cache']}/{preset}/{file}";
47
48 15
        $this->app['router']->get(
49 10
            $url,
50
            function ($preset, $file) {
51
                try {
52 12
                    $final_file = $this->app['imagecache']->handleRequest($preset, $file);
53 11
                } catch (InvalidPresetException $e) {
54 3
                    return \Response::make('Invalid preset', 404);
55 6
                } catch (NotFoundException $e) {
56 3
                    return \Response::make('File not found', 404);
57 3
                } catch (\RuntimeException $e) {
58 3
                    return \Response::make($e->getMessage(), 500);
59
                }
60
61 3
                $transfer = new Transfer($final_file);
62
63 3
                $callback = function () use ($transfer) {
64 3
                    $transfer->stream();
65 3
                };
66
67 3
                return \Response::stream($callback, $transfer->getStatus(), $transfer->getHeaders());
68 5
            }
69 15
        )->where('file', '.*');
70 15
    }
71
72
    /**
73
     * Register the service provider.
74
     *
75
     * @return void
76
     */
77 15
    public function register()
78
    {
79 15
        $this->registerConfiguration();
80
81 15
        $this->registerManager();
82
83 15
        $this->registerRoute();
84 15
    }
85
}
86