Completed
Push — master ( 4debaf...52a093 )
by Iurii
02:33
created

Main::getApiModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Google API
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0-or-later
8
 */
9
10
namespace gplcart\modules\gapi;
11
12
use Exception;
13
use gplcart\core\Config;
14
use gplcart\core\Library;
15
use gplcart\core\Container;
16
17
/**
18
 * Main class for Google API module
19
 */
20
class Main
21
{
22
23
    /**
24
     * Database class instance
25
     * @var \gplcart\core\Database $db
26
     */
27
    protected $db;
28
29
    /**
30
     * Library class instance
31
     * @var \gplcart\core\Library $library
32
     */
33
    protected $library;
34
35
    /**
36
     * @param Config $config
37
     * @param Library $library
38
     */
39
    public function __construct(Config $config, Library $library)
40
    {
41
        $this->library = $library;
42
        $this->db = $config->getDb();
43
        $this->db->addScheme($this->getDbScheme());
44
    }
45
46
    /**
47
     * Implements hook "library.list"
48
     * @param array $libraries
49
     */
50
    public function hookLibraryList(array &$libraries)
51
    {
52
        $libraries['gapi'] = array(
53
            'name' => 'Google API', // @text
54
            'description' => 'A PHP client library for accessing Google APIs', // @text
55
            'type' => 'php',
56
            'module' => 'gapi',
57
            'url' => 'https://github.com/google/google-api-php-client',
58
            'download' => 'https://github.com/google/google-api-php-client/releases/download/v2.2.1/google-api-php-client-2.2.1_PHP54.zip',
59
            'version_source' => array(
60
                'file' => 'vendor/google/apiclient/src/Google/Client.php',
61
                'pattern' => '/LIBVER(?:.*)"(.*)"/',
62
            ),
63
            'files' => array(
64
                'vendor/autoload.php',
65
            )
66
        );
67
    }
68
69
    /**
70
     * Implements hook "module.enable.after"
71
     */
72
    public function hookModuleEnableAfter()
73
    {
74
        $this->library->clearCache();
75
    }
76
77
    /**
78
     * Implements hook "module.disable.after"
79
     */
80
    public function hookModuleDisableAfter()
81
    {
82
        $this->library->clearCache();
83
    }
84
85
    /**
86
     * Implements hook "module.install.before"
87
     * @param null|string
88
     */
89
    public function hookModuleInstallBefore(&$result)
90
    {
91
        try {
92
            $this->db->importScheme('module_gapi_credential', $this->getDbScheme());
93
        } catch (Exception $ex) {
94
            $result = $ex->getMessage();
95
        }
96
    }
97
98
    /**
99
     * Implements hook "module.install.after"
100
     */
101
    public function hookModuleInstallAfter()
102
    {
103
        $this->library->clearCache();
104
    }
105
106
    /**
107
     * Implements hook "module.uninstall.after"
108
     */
109
    public function hookModuleUninstallAfter()
110
    {
111
        $this->library->clearCache();
112
        $this->db->deleteTable('module_gapi_credential');
113
    }
114
115
    /**
116
     * Implements hook "route.list"
117
     * @param mixed $routes
118
     */
119
    public function hookRouteList(array &$routes)
120
    {
121
        $routes['admin/report/gapi'] = array(
122
            'menu' => array(
123
                'admin' => 'Google API credentials' // @text
124
            ),
125
            'access' => 'module_gapi_credential',
126
            'handlers' => array(
127
                'controller' => array('gplcart\\modules\\gapi\\controllers\\Credential', 'listCredential')
128
            )
129
        );
130
131
        $routes['admin/module/settings/gapi/credential/add/(\w+)'] = array(
132
            'access' => 'module_gapi_credential_add',
133
            'handlers' => array(
134
                'controller' => array('gplcart\\modules\\gapi\\controllers\\Credential', 'addCredential')
135
            )
136
        );
137
138
        $routes['admin/module/settings/gapi/credential/edit/(\d+)'] = array(
139
            'access' => 'module_gapi_credential_edit',
140
            'handlers' => array(
141
                'controller' => array('gplcart\\modules\\gapi\\controllers\\Credential', 'editCredential')
142
            )
143
        );
144
    }
145
146
    /**
147
     * Implements hook "user.role.permissions"
148
     * @param array $permissions
149
     */
150
    public function hookUserRolePermissions(array &$permissions)
151
    {
152
        $permissions['module_gapi_credential'] = 'Google API credential: access'; // @text
153
        $permissions['module_gapi_credential_add'] = 'Google API credential: add'; // @text
154
        $permissions['module_gapi_credential_edit'] = 'Google API credential: edit'; // @text
155
        $permissions['module_gapi_credential_delete'] = 'Google API credential: delete'; // @text
156
    }
157
158
    /**
159
     * Returns Google API credential
160
     * @param int $id
161
     * @return array
162
     */
163
    public function getCredential($id)
164
    {
165
        return $this->getCredentialModel()->get($id);
166
    }
167
168
    /**
169
     * Returns an array of existing Google API credentials
170
     * @param array $options
171
     * @return array
172
     */
173
    public function getCredentials(array $options = array())
174
    {
175
        return $this->getCredentialModel()->getList($options);
176
    }
177
178
    /**
179
     * Returns Google Client object
180
     * @param array $config
181
     * @param null|int $credential_id
182
     * @return \Google_Client
183
     */
184
    public function getGoogleClient(array $config = array(), $credential_id)
185
    {
186
        return $this->getApiModel()->getClient($config, $credential_id);
187
    }
188
189
    /**
190
     * Returns a Google service class instance
191
     * @param string $service_name
192
     * @param \Google_Client $client
193
     * @return object \Google_Service_SERVICE-NAME
194
     */
195
    public function getGoogleService($service_name, \Google_Client $client)
196
    {
197
        return $this->getApiModel()->getService($service_name, $client);
198
    }
199
200
    /**
201
     * Returns an array of Google service names supported by the library
202
     */
203
    public function getGoogleServiceNames()
204
    {
205
        return $this->getApiModel()->getServiceNames();
206
    }
207
208
    /**
209
     * Returns the Credential model instance
210
     * @return \gplcart\modules\gapi\models\Credential
211
     */
212
    protected function getCredentialModel()
213
    {
214
        /** @var \gplcart\modules\gapi\models\Credential $instance */
215
        $instance = Container::get('gplcart\\modules\\gapi\\models\\Credential');
216
        return $instance;
217
    }
218
219
    /**
220
     * Returns the API model instance
221
     * @return \gplcart\modules\gapi\models\Api
222
     */
223
    protected function getApiModel()
224
    {
225
        /** @var \gplcart\modules\gapi\models\Api $instance */
226
        $instance = Container::get('gplcart\\modules\\gapi\\models\\Api');
227
        return $instance;
228
    }
229
230
    /**
231
     * Returns an array of database scheme
232
     * @return array
233
     */
234
    protected function getDbScheme()
235
    {
236
        return array(
237
            'module_gapi_credential' => array(
238
                'fields' => array(
239
                    'created' => array('type' => 'int', 'length' => 10, 'not_null' => true),
240
                    'name' => array('type' => 'varchar', 'length' => 255, 'not_null' => true),
241
                    'type' => array('type' => 'varchar', 'length' => 255, 'not_null' => true),
242
                    'data' => array('type' => 'blob', 'not_null' => true, 'serialize' => true),
243
                    'modified' => array('type' => 'int', 'length' => 10, 'not_null' => true, 'default' => 0),
244
                    'credential_id' => array('type' => 'int', 'length' => 10, 'auto_increment' => true, 'primary' => true)
245
                )
246
            )
247
        );
248
    }
249
}
250