Completed
Pull Request — dev (#25)
by nonanerz
02:26
created

GoogleClientFactory::createCalendar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 0
cts 4
cp 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace AppBundle\Services;
4
5
class GoogleClientFactory
6
{
7
    private $path;
8
9
    private $scope = 'default';
10
11
    private $role = 'reader';
12
13
    public function createCalendar()
14
    {
15
        $calendar = new \Google_Service_Calendar($this->createClient());
16
17
        $calendar->acl->insert('primary', $this->scope());
18
19
        return $calendar;
20
    }
21
22
    private function createClient()
23
    {
24
        $client = new \Google_Client();
25
        $client->setScopes([\Google_Service_Calendar::CALENDAR]);
26
        $path = $this->getCredentials();
27
        $client->setAuthConfig($path);
28
29
        return $client;
30
    }
31
32
    private function scope()
33
    {
34
        $scope = new \Google_Service_Calendar_AclRuleScope();
35
        $scope->setType($this->scope);
36
37
        $rule = new \Google_Service_Calendar_AclRule();
38
        $rule->setRole($this->role);
39
        $rule->setScope($scope);
40
41
        return $rule;
42
    }
43
44
    private function getCredentials()
45
    {
46
        if (file_exists(__DIR__.'/../../../app/config/credentials/credentials.json')) {
47
            $this->path = __DIR__.'/../../../app/config/credentials/credentials.json';
48
49
            return $this->path;
50
        } else {
51
            throw new \Google_Exception('Credentials is not valid');
52
        }
53
    }
54
55
    public function setScope($type, $role)
56
    {
57
        $this->scope = $type;
58
59
        $this->role = $role;
60
    }
61
}
62