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