Completed
Push — master ( 2f550b...c77d52 )
by Stéphane
03:09
created

ImagecacheServiceProvider::registerConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
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(
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