Completed
Push — master ( cacc5b...d7ee0c )
by Christopher
01:15
created

ProjectsServiceProvider::bootEvents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Chriscreates\Projects;
4
5
use Chriscreates\Projects\Commands\ProjectsConfigCommand;
6
use Chriscreates\Projects\Commands\ProjectsSeederCommand;
7
use Illuminate\Contracts\Container\BindingResolutionException;
8
use Illuminate\Database\Eloquent\Factory;
9
use Illuminate\Events\Dispatcher;
10
use Illuminate\Support\ServiceProvider;
11
12
class ProjectsServiceProvider extends ServiceProvider
13
{
14
15
    /**
16
       * All of the event / listener mappings.
17
       *
18
       * @var array
19
       */
20
    protected $events = [];
21
22
    /**
23
     * Bootstrap the application services.
24
     */
25
    public function boot()
26
    {
27
        $this->bootEvents();
28
29
        $this->bootFactories(projects_base_path('/database/factories'));
30
31
        $this->handleMigrations();
32
    }
33
34
    protected function bootFactories($path)
35
    {
36
        $this->app->make(Factory::class)->load($path);
37
    }
38
39
    /**
40
     * Register bindings in the container.
41
     *
42
     * @return void
43
     */
44
    public function register()
45
    {
46
        $this->registerCommands();
47
48
        foreach (glob(__DIR__.'/Helpers/*.php') as $file) {
49
            require_once($file);
50
        }
51
    }
52
53
    /**
54
     * Register the events and listeners.
55
     *
56
     * @return void
57
     * @throws BindingResolutionException
58
     */
59
    private function bootEvents()
60
    {
61
        $events = $this->app->make(Dispatcher::class);
62
63
        foreach ($this->events as $event => $listeners) {
64
            foreach ($listeners as $listener) {
65
                $events->listen($event, $listener);
66
            }
67
        }
68
    }
69
70
    /**
71
     * Register the package's migrations.
72
     *
73
     * @return void
74
     */
75
    private function handleMigrations()
76
    {
77
        if ($this->app->runningUnitTests()) {
78
            $this->loadMigrationsFrom(projects_base_path('/database/test_migrations'));
79
        }
80
81
        if ($this->app->runningInConsole()) {
82
            $this->loadMigrationsFrom(projects_base_path('/database/migrations'));
83
        }
84
    }
85
86
    /**
87
     * @return void
88
     */
89
    private function registerCommands()
90
    {
91
        $this->commands([
92
            ProjectsConfigCommand::class,
93
            ProjectsSeederCommand::class,
94
        ]);
95
    }
96
}
97