Completed
Pull Request — master (#44)
by
unknown
08:54
created

GoogleCalendarServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Spatie\GoogleCalendar;
4
5
use Illuminate\Support\ServiceProvider;
6
use Spatie\GoogleCalendar\Exceptions\InvalidConfiguration;
7
8
class GoogleCalendarServiceProvider extends ServiceProvider
9
{
10
    /**
11
     * Bootstrap the application services.
12
     */
13
    public function boot()
14
    {
15
        $this->publishes([
16
            __DIR__ . '/../config/laravel-google-calendar.php' => config_path('laravel-google-calendar.php'),
17
        ], 'config');
18
    }
19
20
    /**
21
     * Register the application services.
22
     */
23
    public function register()
24
    {
25
        $this->mergeConfigFrom(__DIR__ . '/../config/laravel-google-calendar.php', 'laravel-google-calendar');
26
27
        $this->app->bind(GoogleCalendar::class, function () {
28
            $config = config('laravel-google-calendar');
29
30
            $this->guardAgainstInvalidConfiguration($config);
31
32
            return GoogleCalendarFactory::createForCalendarId($config['calendar_id']);
33
        });
34
35
        $this->app->alias(GoogleCalendar::class, 'laravel-google-calendar');
36
    }
37
38
    /**
39
     * @param array|null $config
40
     *
41
     * @throws \Spatie\GoogleCalendar\Exceptions\InvalidConfiguration
42
     */
43
    protected function guardAgainstInvalidConfiguration(array $config = null)
44
    {
45
        if (empty($config['calendar_id'])) {
46
            throw InvalidConfiguration::calendarIdNotSpecified();
47
        }
48
49
        if (! file_exists($config['service_account_credentials_json'])) {
50
            throw InvalidConfiguration::credentialsJsonDoesNotExist($config['service_account_credentials_json']);
51
        }
52
    }
53
}
54