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