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

GoogleCalendarFactory::createCalendarClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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