Completed
Push — master ( 347a3f...e08dc2 )
by Iurii
03:52 queued 43s
created

Module   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 135
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hookRouteList() 0 17 1
A hookUserRolePermissions() 0 4 1
A hookOauthProviders() 0 16 1
B hookDashboardHandlers() 0 29 3
A hookConstructControllerBackend() 0 8 2
A getHandlers() 0 4 1
A getModel() 0 4 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\Container,
13
    gplcart\core\Module as CoreModule;
14
15
/**
16
 * Main class for Google Analytics Report module
17
 */
18
class Module
19
{
20
21
    /**
22
     * Model class instance
23
     * @var \gplcart\core\Model $model
24
     */
25
    protected $module;
26
27
    /**
28
     * @param CoreModule $module
29
     */
30
    public function __construct(CoreModule $module)
31
    {
32
        $this->module = $module;
0 ignored issues
show
Documentation Bug introduced by
It seems like $module of type object<gplcart\core\Module> is incompatible with the declared type object<gplcart\core\Model> of property $module.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
34
35
    /**
36
     * Implements hook "route.list"
37
     * @param array $routes
38
     */
39
    public function hookRouteList(array &$routes)
40
    {
41
        $routes['admin/module/settings/ga_report'] = array(
42
            'access' => 'module_edit',
43
            'handlers' => array(
44
                'controller' => array('gplcart\\modules\\ga_report\\controllers\\Settings', 'editSettings')
45
            )
46
        );
47
48
        $routes['admin/report/ga'] = array(
49
            'menu' => array('admin' => /* @text */'Google Analytics'),
50
            'access' => 'ga_report',
51
            'handlers' => array(
52
                'controller' => array('gplcart\\modules\\ga_report\\controllers\\Report', 'listReport')
53
            )
54
        );
55
    }
56
57
    /**
58
     * Implements hook "user.role.permissions"
59
     * @param array $permissions
60
     */
61
    public function hookUserRolePermissions(array &$permissions)
62
    {
63
        $permissions['ga_report'] = /* @text */'Google Analytics Report: access';
64
    }
65
66
    /**
67
     * Implements hook "oauth.providers"
68
     * @param array $providers
69
     */
70
    public function hookOauthProviders(array &$providers)
71
    {
72
        $providers['ga'] = array(
73
            'name' => /* @text */'Google Analytics',
74
            'settings' => $this->module->getSettings('ga_report'),
75
            'url' => array(
76
                'process' => 'https://www.googleapis.com/analytics/v3/data/ga',
77
                'token' => 'https://www.googleapis.com/oauth2/v4/token'
78
            ),
79
            'scope' => 'https://www.googleapis.com/auth/analytics.readonly',
80
            'handlers' => array(
81
                'token' => array('gplcart\\modules\\ga_report\\handlers\\Api', 'token'),
82
                'process' => array('gplcart\\modules\\ga_report\\handlers\\Api', 'process'),
83
            )
84
        );
85
    }
86
87
    /**
88
     * Implements hook "dashboard.handlers"
89
     * @param array $handlers
90
     */
91
    public function hookDashboardHandlers(array &$handlers)
92
    {
93
        $weight = count($handlers);
94
        $model = $this->getModel();
95
        $settings = $this->module->getSettings('ga_report');
96
97
        foreach ($model->getHandlers() as $id => $handler) {
98
99
            if (!in_array($id, $settings['dashboard'])) {
100
                continue;
101
            }
102
103
            $weight++;
104
105
            $report = $model->get($id, $settings);
106
107
            $handlers["ga_$id"] = array(
108
                'status' => true,
109
                'weight' => $weight,
110
                'title' => $handler['name'],
111
                'template' => $handler['template'],
112
                'handlers' => array(
113
                    'data' => function() use ($report, $settings) {
114
                        return array('report' => $report, 'settings' => $settings);
115
                    }
116
                )
117
            );
118
        }
119
    }
120
121
    /**
122
     * Implements hook "construct.controller.backend"
123
     * @param \gplcart\core\controllers\backend\Controller $controller
124
     */
125
    public function hookConstructControllerBackend($controller)
126
    {
127
        if ($controller->isQuery('ga.update')) {
128
            $store_id = $controller->getQuery('ga.update.store_id', '');
129
            $handler_id = $controller->getQuery('ga.update.handler_id', '');
130
            $this->getModel()->clearCache($handler_id, $store_id);
131
        }
132
    }
133
134
    /**
135
     * Returns an array of GA handlers
136
     * @return array
137
     */
138
    public function getHandlers()
139
    {
140
        return $this->getModel()->getHandlers();
141
    }
142
143
    /**
144
     * Returns the report model instance
145
     * @return \gplcart\modules\ga_report\models\Report
146
     */
147
    protected function getModel()
148
    {
149
        return Container::get('gplcart\\modules\\ga_report\\models\\Report');
150
    }
151
152
}
153