ImgAttacherServiceProvider::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace CbCaio\ImgAttacher\Providers;
4
5
use CbCaio\ImgAttacher\Managers\FilePathManager;
6
use CbCaio\ImgAttacher\Models\AttacherImage;
7
use Illuminate\Support\ServiceProvider;
8
9
class ImgAttacherServiceProvider extends ServiceProvider
10
{
11
    /**
12
     * Indicates if loading of the provider is deferred.
13
     *
14
     * @var bool
15
     */
16
    protected $defer = FALSE;
17
18
    /**
19
     * Perform post-registration booting of services.
20
     *
21
     * @return void
22
     */
23
    public function boot()
24
    {
25
        //$migration_timestamp = \Carbon\Carbon::now()->format('Y_m_d') . '_000000_';
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
        $migration_file_name = '2016_01_12_000000_create_attacher_images_table.php';
27
28
        $this->publishes(
29
            [
30
                __DIR__ . '/../../resources/config/img-attacher.php'
31
                => config_path('img-attacher.php'),
32
                __DIR__ . '/../../resources/database/migrations/' . $migration_file_name
33
                => database_path('migrations/'. $migration_file_name),
34
            ]
35
        );
36
37
        $this->mergeConfigFrom(
38
            __DIR__ . '/../../resources/config/img-attacher.php', 'img-attacher'
39
        );
40
41
        $this->registerEvents();
42
    }
43
44
    /**
45
     * Register any package services.
46
     *
47
     * @return void
48
     */
49
    public function register()
50
    {
51
        $this->registerBindings();
52
        $this->registerDependencies();
53
        $this->registerAttacherManager();
54
        $this->registerFilePathManager();
55
        $this->registerFileManager();
56
        $this->registerImageProcessor();
57
    }
58
59
    private function registerEvents()
60
    {
61
        AttacherImage::saving(function ($model) {
62
            if ( !  app('img-attacher')->writeImage($model)) {
63
                return false;
64
            }
65
            return true;
66
        });
67
68
        AttacherImage::deleting(function ($model) {
69
            if ( !  app('img-attacher')->deleteImages($model)) {
70
                return false;
71
            }
72
            return true;
73
        });
74
75
    }
76
77
    private function registerAttacherManager()
78
    {
79
        $this->app->singleton('img-attacher', 'CbCaio\ImgAttacher\AttacherManager');
80
    }
81
82
83
    private function registerBindings()
84
    {
85
        $this->app->bind('CbCaio\ImgAttacher\Contracts\AttacherImageContract',
86
                         'CbCaio\ImgAttacher\Models\AttacherImage');
87
        $this->app->bind('CbCaio\ImgAttacher\Contracts\FilePathManagerContract',
88
                         'CbCaio\ImgAttacher\Managers\FilePathManager');
89
        $this->app->bind('CbCaio\ImgAttacher\Contracts\FileManagerContract',
90
                         'CbCaio\ImgAttacher\Managers\FileManager');
91
    }
92
93
    private function registerFileManager()
94
    {
95
        $this->app->singleton('img-attacher.FileManager', 'CbCaio\ImgAttacher\Managers\FileManager');
96
    }
97
98
    private function registerFilePathManager()
99
    {
100
        $this->app->singleton('img-attacher.FilePathManager', function ()
101
        {
102
            $config = config('img-attacher');
103
104
            return new FilePathManager($config['path_to_save'],$config['base_url'], $config['processing_styles_routines']);
105
        });
106
    }
107
108
    private function registerImageProcessor()
109
    {
110
        $this->app->singleton('img-attacher.ImageProcessor','CbCaio\ImgAttacher\Processors\ImageProcessor' );
111
    }
112
113
    private function registerDependencies()
114
    {
115
        $this->app->register(\GrahamCampbell\Flysystem\FlysystemServiceProvider::class);
116
        $this->app->register(\Intervention\Image\ImageServiceProvider::class);
117
    }
118
119
}