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

GoogleCalendarServiceProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A register() 0 14 1
A guardAgainstInvalidConfiguration() 0 10 3
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