1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Google API |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2015, Iurii Makukh |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\gapi\models; |
11
|
|
|
|
12
|
|
|
use gplcart\core\Library; |
13
|
|
|
use RuntimeException; |
14
|
|
|
use OutOfRangeException; |
15
|
|
|
use gplcart\core\exceptions\Dependency as DependencyException; |
16
|
|
|
use gplcart\modules\gapi\models\Credential as ModuleGapiCredentialModel; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Manages basic behaviors and data related Google API methods |
20
|
|
|
*/ |
21
|
|
|
class Api |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Library class instance |
26
|
|
|
* @var \gplcart\core\Library $library |
27
|
|
|
*/ |
28
|
|
|
protected $library; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Credential model instance |
32
|
|
|
* @var \gplcart\modules\gapi\models\Credential $credential |
33
|
|
|
*/ |
34
|
|
|
protected $credential; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Library $library |
38
|
|
|
* @param ModuleGapiCredentialModel $credential |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Library $library, ModuleGapiCredentialModel $credential) |
41
|
|
|
{ |
42
|
|
|
$this->library = $library; |
43
|
|
|
$this->credential = $credential; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns Google Client object |
48
|
|
|
* @param null|int $credential_id |
49
|
|
|
* @param bool $use_own_certificate |
50
|
|
|
* @return \Google_Client |
51
|
|
|
* @throws DependencyException |
52
|
|
|
* @throws OutOfRangeException |
53
|
|
|
*/ |
54
|
|
|
public function getClient($credential_id = null, $use_own_certificate = true) |
55
|
|
|
{ |
56
|
|
|
$this->library->load('gapi'); |
57
|
|
|
|
58
|
|
|
if (!class_exists('Google_Client')) { |
59
|
|
|
throw new DependencyException('Class \Google_Client not found'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$client = new \Google_Client; |
63
|
|
|
|
64
|
|
|
if ($use_own_certificate) { |
65
|
|
|
$http = new \GuzzleHttp\Client(array('verify' => __DIR__ . '/../certificates/cacert.pem')); |
66
|
|
|
$client->setHttpClient($http); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (isset($credential_id)) { |
70
|
|
|
$credential = $this->credential->get($credential_id); |
71
|
|
|
if (empty($credential['data']['file'])) { |
72
|
|
|
throw new OutOfRangeException('Credential file path is empty'); |
73
|
|
|
} |
74
|
|
|
$client->setAuthConfig(gplcart_file_absolute($credential['data']['file'])); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
return $client; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns a Google service class instance |
83
|
|
|
* @param string $service_name |
84
|
|
|
* @param \Google_Client $client |
85
|
|
|
* @return object |
86
|
|
|
* @throws DependencyException |
87
|
|
|
*/ |
88
|
|
|
public function getService($service_name, \Google_Client $client) |
89
|
|
|
{ |
90
|
|
|
$this->library->load('gapi'); |
91
|
|
|
|
92
|
|
|
$class = "Google_Service_$service_name"; |
93
|
|
|
|
94
|
|
|
if (!class_exists($class)) { |
95
|
|
|
throw new DependencyException("Class $class not found"); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return new $class($client); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns an array of Google service names supported by the library |
103
|
|
|
* @return array |
104
|
|
|
* @throws RuntimeException |
105
|
|
|
*/ |
106
|
|
|
public function getServiceNames() |
107
|
|
|
{ |
108
|
|
|
$dir = __DIR__ . '/../vendor/google/apiclient-services/src/Google/Service'; |
109
|
|
|
|
110
|
|
|
if (!is_dir($dir) || !is_readable($dir)) { |
111
|
|
|
throw new RuntimeException("Cannot read directory $dir"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->library->load('gapi'); |
115
|
|
|
|
116
|
|
|
$names = array(); |
117
|
|
|
|
118
|
|
|
foreach (glob("$dir/*.php") as $file) { |
119
|
|
|
$name = pathinfo($file, PATHINFO_FILENAME); |
120
|
|
|
$class = "Google_Service_$name"; |
121
|
|
|
if (class_exists($class) && $class instanceof \Google_Service) { |
122
|
|
|
$names[] = $name; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $names; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|