Completed
Push — master ( 876046...6e04bc )
by Joao
02:55
created

blogServiceProvider   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
lcom 1
cbo 5
dl 0
loc 134
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 4 1
A register() 0 7 1
B prepareResources() 0 28 1
B registerBlogPost() 0 19 5
A registerBlogCategory() 0 14 3
A registerBlog() 0 10 1
A provides() 0 8 1
1
<?php namespace jlourenco\blog;
2
3
use Illuminate\Support\ServiceProvider;
4
use jlourenco\blog\Repositories\BlogCategoryRepository;
5
use jlourenco\blog\Repositories\BlogPostRepository;
6
7
class blogServiceProvider extends ServiceProvider
8
{
9
10
    /**
11
     * Perform post-registration booting of services.
12
     *
13
     * @return void
14
     */
15
    public function boot()
16
    {
17
18
    }
19
20
    /**
21
     * Register any package services.
22
     *
23
     * @return void
24
     */
25
    public function register()
26
    {
27
        $this->prepareResources();
28
        $this->registerBlogCategory();
29
        $this->registerBlogPost();
30
        $this->registerBlog();
31
    }
32
33
    /**
34
     * Prepare the package resources.
35
     *
36
     * @return void
37
     */
38
    protected function prepareResources()
39
    {
40
        // Publish our views
41
        $this->loadViewsFrom(base_path("resources/views"), 'base');
42
        $this->publishes([
43
            __DIR__ .  '/views' => base_path("resources/views")
44
        ]);
45
46
        // Publish our lang
47
        $this->publishes([
48
            __DIR__ .  '/lang' => base_path("resources/lang")
49
        ], 'migrations');
50
51
        // Publish our migrations
52
        $this->publishes([
53
            __DIR__ .  '/migrations' => base_path("database/migrations")
54
        ], 'migrations');
55
56
        // Publish a config file
57
        $this->publishes([
58
            __DIR__ . '/config' => base_path('/config')
59
        ], 'config');
60
61
        // Publish our routes
62
        $this->publishes([
63
            __DIR__ .  '/routes.php' => base_path("app/Http/base_routes.php")
64
        ], 'routes');
65
    }
66
67
    /**
68
     * Registers the blog posts.
69
     *
70
     * @return void
71
     */
72
    protected function registerBlogPost()
73
    {
74
        $this->app->singleton('jlourenco.blog.post', function ($app) {
75
            $baseConfig = $app['config']->get('jlourenco.base');
76
            $config = $app['config']->get('jlourenco.blog');
77
78
            $model = array_get($config, 'models.BlogPost');
79
            $users = array_get($baseConfig, 'models.User');
80
            $categories = array_get($config, 'models.BlogCategory');
81
82
            if (class_exists($model) && method_exists($model, 'setUsersModel'))
83
                forward_static_call_array([$model, 'setUsersModel'], [$users]);
84
85
            if (class_exists($model) && method_exists($model, 'setCategoriesModel'))
86
                forward_static_call_array([$model, 'setCategoriesModel'], [$categories]);
87
88
            return new BlogPostRepository($model);
89
        });
90
    }
91
92
    /**
93
     * Registers the blog posts.
94
     *
95
     * @return void
96
     */
97
    protected function registerBlogCategory()
98
    {
99
        $this->app->singleton('jlourenco.blog.category', function ($app) {
100
            $config = $app['config']->get('jlourenco.blog');
101
102
            $model = array_get($config, 'models.BlogCategory');
103
            $posts = array_get($config, 'models.BlogPost');
104
105
            if (class_exists($model) && method_exists($model, 'setPostsModel'))
106
                forward_static_call_array([$model, 'setPostsModel'], [$posts]);
107
108
            return new BlogCategoryRepository($model);
109
        });
110
    }
111
112
    /**
113
     * Registers log.
114
     *
115
     * @return void
116
     */
117
    protected function registerBlog()
118
    {
119
        $this->app->singleton('blog', function ($app) {
120
            $blog = new Blog($app['jlourenco.blog.post'], $app['jlourenco.blog.category']);
121
122
            return $blog;
123
        });
124
125
        $this->app->alias('blog', 'jlourenco\blog\Blog');
126
    }
127
128
    /**
129
     * {@inheritDoc}
130
     */
131
    public function provides()
132
    {
133
        return [
134
            'jlourenco.blog.post',
135
            'jlourenco.blog.category',
136
            'blog'
137
        ];
138
    }
139
140
}