Completed
Push — master ( 830e9e...1d6fcc )
by Arjay
08:17
created

AddressServiceProvider::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
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->setupRoutes();
42
43
        $this->setupMacro();
44
    }
45
46
    protected function setupRoutes()
47
    {
48
        Route::group([
49
            'prefix'     => config('address.prefix'),
50
            'middleware' => config('address.middleware'),
51
            'as'         => 'address.',
52
        ], function () {
53
            Route::get('regions', RegionsController::class . '@all')->name('regions.all');
54
            Route::get('provinces', ProvincesController::class . '@all')->name('provinces.all');
55
            Route::get('provinces/{regionId}', ProvincesController::class . '@getByRegion')->name('provinces.region');
56
            Route::get('cities/{provinceId}', CitiesController::class . '@getByProvince')->name('cities.province');
57
            Route::get('cities/{regionId}/{provinceId}', CitiesController::class . '@getByRegionAndProvince')
58
                 ->name('cities.region.province');
59
            Route::get('barangays/{cityId}', BarangaysController::class . '@getByCity')->name('barangay.city');
60
        });
61
    }
62
63
    protected function setupMacro()
64
    {
65
        Blueprint::macro('address', function () {
66
            $this->string('street');
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...
67
            $this->string('barangay_id', 10)->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...
68
            $this->string('city_id', 10)->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...
69
            $this->string('province_id', 10)->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...
70
            $this->string('region_id', 10)->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...
71
        });
72
73
        Blueprint::macro('dropAddress', function () {
74
            $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...
75
        });
76
    }
77
78
    /**
79
     * Register services.
80
     *
81
     * @return void
82
     */
83
    public function register()
84
    {
85
        $this->app->singleton(RegionsRepository::class, function () {
86
            return new CachingRegionsRepository(
87
                $this->app->make(RegionsRepositoryEloquent::class),
88
                $this->app['cache.store']
89
            );
90
        });
91
        $this->app->singleton(ProvincesRepository::class, function () {
92
            return new CachingProvincesRepository(
93
                $this->app->make(ProvincesRepositoryEloquent::class),
94
                $this->app['cache.store']
95
            );
96
        });
97
        $this->app->singleton(CitiesRepository::class, function () {
98
            return new CachingCitiesRepository(
99
                $this->app->make(CitiesRepositoryEloquent::class),
100
                $this->app['cache.store']
101
            );
102
        });
103
        $this->app->singleton(BarangaysRepository::class, function () {
104
            return new CachingBarangaysRepository(
105
                $this->app->make(BarangaysRepositoryEloquent::class),
106
                $this->app['cache.store']
107
            );
108
        });
109
    }
110
}
111