Completed
Pull Request — dev (#25)
by nonanerz
03:34
created

GoogleClientFactory::getCredentials()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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