Completed
Push — master ( 1241b8...7f0603 )
by Iurii
01:18
created

Main::hookModuleInstallBefore()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * @package Google Analytics Report
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\ga_report;
11
12
use gplcart\core\Module,
13
    gplcart\core\Container;
14
15
/**
16
 * Main class for Google Analytics Report module
17
 */
18
class Main
19
{
20
21
    /**
22
     * Module class instance
23
     * @var \gplcart\core\Module $model
24
     */
25
    protected $module;
26
27
    /**
28
     * @param Module $module
29
     */
30
    public function __construct(Module $module)
31
    {
32
        $this->module = $module;
33
    }
34
35
    /**
36
     * Implements hook "module.install.before"
37
     * @param null|string
38
     */
39
    public function hookModuleInstallBefore(&$result)
40
    {
41
        if (!extension_loaded('openssl')) {
42
            /* @var $translation \gplcart\core\models\Translation */
43
            $translation = gplcart_instance_model('Translation');
44
            $result = $translation->text('OpenSSL extension is not enabled');
45
        }
46
    }
47
48
    /**
49
     * Implements hook "route.list"
50
     * @param array $routes
51
     */
52
    public function hookRouteList(array &$routes)
53
    {
54
        $routes['admin/module/settings/ga_report'] = array(
55
            'access' => 'module_edit',
56
            'handlers' => array(
57
                'controller' => array('gplcart\\modules\\ga_report\\controllers\\Settings', 'editSettings')
58
            )
59
        );
60
61
        $routes['admin/report/ga'] = array(
62
            'menu' => array('admin' => /* @text */'Google Analytics'),
63
            'access' => 'ga_report',
64
            'handlers' => array(
65
                'controller' => array('gplcart\\modules\\ga_report\\controllers\\Report', 'listReport')
66
            )
67
        );
68
    }
69
70
    /**
71
     * Implements hook "user.role.permissions"
72
     * @param array $permissions
73
     */
74
    public function hookUserRolePermissions(array &$permissions)
75
    {
76
        $permissions['ga_report'] = /* @text */'Google Analytics Report: access';
77
    }
78
79
    /**
80
     * Implements hook "oauth.providers"
81
     * @param array $providers
82
     */
83
    public function hookOauthProviders(array &$providers)
84
    {
85
        $providers['ga'] = array(
86
            'name' => /* @text */'Google Analytics',
87
            'settings' => $this->module->getSettings('ga_report'),
88
            'url' => array(
89
                'process' => 'https://www.googleapis.com/analytics/v3/data/ga',
90
                'token' => 'https://www.googleapis.com/oauth2/v4/token'
91
            ),
92
            'scope' => 'https://www.googleapis.com/auth/analytics.readonly',
93
            'handlers' => array(
94
                'token' => array('gplcart\\modules\\ga_report\\handlers\\Api', 'token'),
95
                'process' => array('gplcart\\modules\\ga_report\\handlers\\Api', 'process'),
96
            )
97
        );
98
    }
99
100
    /**
101
     * Implements hook "dashboard.handlers"
102
     * @param array $handlers
103
     */
104
    public function hookDashboardHandlers(array &$handlers)
105
    {
106
        $weight = count($handlers);
107
        $model = $this->getModel();
108
        $settings = $this->module->getSettings('ga_report');
109
110
        foreach ($model->getHandlers() as $id => $handler) {
111
112
            if (!in_array($id, $settings['dashboard'])) {
113
                continue;
114
            }
115
116
            $weight++;
117
118
            $report = $model->get($id, $settings);
119
120
            $handlers["ga_$id"] = array(
121
                'status' => true,
122
                'weight' => $weight,
123
                'title' => $handler['name'],
124
                'template' => $handler['template'],
125
                'handlers' => array(
126
                    'data' => function() use ($report, $settings) {
127
                        return array('report' => $report, 'settings' => $settings);
128
                    }
129
                )
130
            );
131
        }
132
    }
133
134
    /**
135
     * Implements hook "construct.controller.backend"
136
     * @param \gplcart\core\controllers\backend\Controller $controller
137
     */
138
    public function hookConstructControllerBackend($controller)
139
    {
140
        if ($controller->isQuery('ga.update')) {
141
            $store_id = $controller->getQuery('ga.update.store_id', '');
142
            $handler_id = $controller->getQuery('ga.update.handler_id', '');
143
            $this->getModel()->clearCache($handler_id, $store_id);
144
        }
145
    }
146
147
    /**
148
     * Returns an array of GA handlers
149
     * @return array
150
     */
151
    public function getHandlers()
152
    {
153
        return $this->getModel()->getHandlers();
154
    }
155
156
    /**
157
     * Returns the report model instance
158
     * @return \gplcart\modules\ga_report\models\Report
159
     */
160
    protected function getModel()
161
    {
162
        return Container::get('gplcart\\modules\\ga_report\\models\\Report');
163
    }
164
165
}
166