GoogleCalendarServiceProvider   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 76
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A register() 0 14 1
A guardAgainstInvalidConfiguration() 0 22 4
A validateServiceAccountConfigSettings() 0 6 1
A validateOAuthConfigSettings() 0 10 1
A validateConfigSetting() 0 10 5
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
    public function boot()
11
    {
12
        $this->publishes([
13
            __DIR__.'/../config/google-calendar.php' => config_path('google-calendar.php'),
14
        ], 'config');
15
    }
16
17
    public function register()
18
    {
19
        $this->mergeConfigFrom(__DIR__.'/../config/google-calendar.php', 'google-calendar');
20
21
        $this->app->bind(GoogleCalendar::class, function () {
22
            $config = config('google-calendar');
23
24
            $this->guardAgainstInvalidConfiguration($config);
25
26
            return GoogleCalendarFactory::createForCalendarId($config['calendar_id']);
27
        });
28
29
        $this->app->alias(GoogleCalendar::class, 'laravel-google-calendar');
30
    }
31
32
    protected function guardAgainstInvalidConfiguration(array $config = null)
33
    {
34
        if (empty($config['calendar_id'])) {
35
            throw InvalidConfiguration::calendarIdNotSpecified();
36
        }
37
38
        $authProfile = $config['default_auth_profile'];
39
40
        if ($authProfile === 'service_account') {
41
            $this->validateServiceAccountConfigSettings($config);
42
43
            return;
44
        }
45
46
        if ($authProfile === 'oauth') {
47
            $this->validateOAuthConfigSettings($config);
48
49
            return;
50
        }
51
52
        throw InvalidConfiguration::invalidAuthenticationProfile($authProfile);
53
    }
54
55
    protected function validateServiceAccountConfigSettings(array $config = null)
56
    {
57
        $credentials = $config['auth_profiles']['service_account']['credentials_json'];
58
59
        $this->validateConfigSetting($credentials);
60
    }
61
62
    protected function validateOAuthConfigSettings(array $config = null)
63
    {
64
        $credentials = $config['auth_profiles']['oauth']['credentials_json'];
65
66
        $this->validateConfigSetting($credentials);
67
68
        $token = $config['auth_profiles']['oauth']['token_json'];
69
70
        $this->validateConfigSetting($token);
71
    }
72
73
    protected function validateConfigSetting(string $setting)
74
    {
75
        if (! is_array($setting) && ! is_string($setting)) {
76
            throw InvalidConfiguration::credentialsTypeWrong($setting);
77
        }
78
79
        if (is_string($setting) && ! file_exists($setting)) {
80
            throw InvalidConfiguration::credentialsJsonDoesNotExist($setting);
81
        }
82
    }
83
}
84