AddressServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Yajra\Address;
4
5
use Illuminate\Database\Schema\Blueprint;
6
use Illuminate\Support\Facades\Route;
7
use Illuminate\Support\ServiceProvider;
8
use Yajra\Address\Controllers\BarangaysController;
9
use Yajra\Address\Controllers\CitiesController;
10
use Yajra\Address\Controllers\ProvincesController;
11
use Yajra\Address\Controllers\RegionsController;
12
use Yajra\Address\Repositories\Barangays\BarangaysRepository;
13
use Yajra\Address\Repositories\Barangays\BarangaysRepositoryEloquent;
14
use Yajra\Address\Repositories\Barangays\CachingBarangaysRepository;
15
use Yajra\Address\Repositories\Cities\CachingCitiesRepository;
16
use Yajra\Address\Repositories\Cities\CitiesRepository;
17
use Yajra\Address\Repositories\Cities\CitiesRepositoryEloquent;
18
use Yajra\Address\Repositories\Provinces\CachingProvincesRepository;
19
use Yajra\Address\Repositories\Provinces\ProvincesRepository;
20
use Yajra\Address\Repositories\Provinces\ProvincesRepositoryEloquent;
21
use Yajra\Address\Repositories\Regions\CachingRegionsRepository;
22
use Yajra\Address\Repositories\Regions\RegionsRepository;
23
use Yajra\Address\Repositories\Regions\RegionsRepositoryEloquent;
24
25
class AddressServiceProvider extends ServiceProvider
26
{
27
    /**
28
     * Bootstrap services.
29
     *
30
     * @return void
31
     */
32
    public function boot()
33
    {
34
        $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
35
36
        $this->mergeConfigFrom($config = __DIR__ . '/../config/address.php', 'address');
37
        if ($this->app->runningInConsole()) {
38
            $this->publishes([$config => config_path('address.php')], 'address');
39
        }
40
41
        $this->setupViews();
42
        $this->setupRoutes();
43
        $this->setupMacro();
44
    }
45
46
    protected function setupViews()
47
    {
48
        view()->composer('address::form', function() {
49
            /** @var RegionsRepository $repo */
50
            $repo = app(RegionsRepository::class);
51
            view()->share('regions', $repo->all()->pluck('name', 'region_id'));
52
        });
53
54
        $this->loadViewsFrom(__DIR__ . '/Views', 'address');
55
56
        $this->publishes([
57
            __DIR__ . '/Views' => resource_path('views/vendor/address'),
58
        ], 'address');
59
    }
60
61
    protected function setupRoutes()
62
    {
63
        Route::group([
64
            'prefix'     => config('address.prefix'),
65
            'middleware' => config('address.middleware'),
66
            'as'         => 'address.',
67
        ], function () {
68
            Route::get('regions', RegionsController::class . '@all')->name('regions.all');
69
            Route::get('provinces', ProvincesController::class . '@all')->name('provinces.all');
70
            Route::get('provinces/{regionId}', ProvincesController::class . '@getByRegion')->name('provinces.region');
71
            Route::get('cities/{provinceId}', CitiesController::class . '@getByProvince')->name('cities.province');
72
            Route::get('cities/{regionId}/{provinceId}', CitiesController::class . '@getByRegionAndProvince')
73
                 ->name('cities.region.province');
74
            Route::get('barangays/{cityId}', BarangaysController::class . '@getByCity')->name('barangay.city');
75
        });
76
    }
77
78
    protected function setupMacro()
79
    {
80
        Blueprint::macro('address', function () {
81
            $this->string('street')->nullable();
0 ignored issues
show
Bug introduced by
The method string() does not seem to exist on object<Yajra\Address\AddressServiceProvider>.

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...
82
            $this->string('barangay_id', 9)->nullable()->index();
0 ignored issues
show
Bug introduced by
The method string() does not seem to exist on object<Yajra\Address\AddressServiceProvider>.

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...
83
            $this->string('city_id', 6)->nullable()->index();
0 ignored issues
show
Bug introduced by
The method string() does not seem to exist on object<Yajra\Address\AddressServiceProvider>.

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...
84
            $this->string('province_id', 4)->nullable()->index();
0 ignored issues
show
Bug introduced by
The method string() does not seem to exist on object<Yajra\Address\AddressServiceProvider>.

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...
85
            $this->string('region_id', 2)->nullable()->index();
0 ignored issues
show
Bug introduced by
The method string() does not seem to exist on object<Yajra\Address\AddressServiceProvider>.

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...
86
        });
87
88
        Blueprint::macro('dropAddress', function () {
89
            $this->dropColumn(['region_id', 'province_id', 'city_id', 'barangay_id', 'street']);
0 ignored issues
show
Bug introduced by
The method dropColumn() does not seem to exist on object<Yajra\Address\AddressServiceProvider>.

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...
90
        });
91
    }
92
93
    /**
94
     * Register services.
95
     *
96
     * @return void
97
     */
98
    public function register()
99
    {
100
        $this->app->singleton(RegionsRepository::class, function () {
101
            return new CachingRegionsRepository(
102
                $this->app->make(RegionsRepositoryEloquent::class),
103
                $this->app['cache.store']
104
            );
105
        });
106
        $this->app->singleton(ProvincesRepository::class, function () {
107
            return new CachingProvincesRepository(
108
                $this->app->make(ProvincesRepositoryEloquent::class),
109
                $this->app['cache.store']
110
            );
111
        });
112
        $this->app->singleton(CitiesRepository::class, function () {
113
            return new CachingCitiesRepository(
114
                $this->app->make(CitiesRepositoryEloquent::class),
115
                $this->app['cache.store']
116
            );
117
        });
118
        $this->app->singleton(BarangaysRepository::class, function () {
119
            return new CachingBarangaysRepository(
120
                $this->app->make(BarangaysRepositoryEloquent::class),
121
                $this->app['cache.store']
122
            );
123
        });
124
    }
125
}
126