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

GoogleClientFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
c 1
b 1
f 0
lcom 1
cbo 6
dl 0
loc 56
ccs 0
cts 26
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createCalendar() 0 8 1
A createClient() 0 9 1
A scope() 0 11 1
A getCredentials() 0 9 2
A setScope() 0 6 1
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
            throw new \Google_Exception('Credentials is not valid');
48
        }
49
        $this->path = __DIR__.'/../../../app/config/credentials/credentials.json';
50
51
        return $this->path;
52
    }
53
54
    public function setScope($type, $role)
55
    {
56
        $this->scope = $type;
57
58
        $this->role = $role;
59
    }
60
}
61