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

GoogleCalendarFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 47
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createForCalendarId() 0 10 1
A createAuthenticatedGoogleClient() 0 12 1
A createCalendarClient() 0 4 1
1
<?php
2
3
namespace Spatie\GoogleCalendar;
4
5
use Google_Client;
6
use Google_Service_Calendar;
7
8
class GoogleCalendarFactory
9
{
10
    /**
11
     * @param string $calendarId
12
     *
13
     * @return \Spatie\GoogleCalendar\GoogleCalendar
14
     */
15
    public static function createForCalendarId(string $calendarId) : GoogleCalendar
16
    {
17
        $config = config('laravel-google-calendar');
18
19
        $client = self::createAuthenticatedGoogleClient($config);
20
21
        $service = new Google_Service_Calendar($client);
22
23
        return self::createCalendarClient($service, $calendarId);
24
    }
25
26
    /**
27
     * @param array $config
28
     *
29
     * @return \Google_Client
30
     */
31
    public static function createAuthenticatedGoogleClient(array $config) : Google_Client
32
    {
33
        $client = new Google_Client;
34
35
        $client->setScopes([
36
            Google_Service_Calendar::CALENDAR,
37
        ]);
38
39
        $client->setAuthConfig($config['service_account_credentials_json']);
40
41
        return $client;
42
    }
43
44
    /**
45
     * @param \Google_Service_Calendar $service
46
     * @param string $calendarId
47
     *
48
     * @return \Spatie\GoogleCalendar\GoogleCalendar
49
     */
50
    protected static function createCalendarClient(Google_Service_Calendar $service, string $calendarId) : GoogleCalendar
51
    {
52
        return new GoogleCalendar($service, $calendarId);
53
    }
54
}
55