Completed
Push — master ( 91133a...41999f )
by Iurii
01:16
created

Main   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 1
dl 0
loc 190
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hookLibraryList() 0 19 1
A hookRouteList() 0 19 1
A hookUserRolePermissions() 0 4 1
A hookOrderCalculateBefore() 0 6 2
A hookOrderSubmitBefore() 0 4 1
A hookShippingMethods() 0 4 1
A hookModuleEnableAfter() 0 4 1
A hookModuleDisableAfter() 0 4 1
A hookModuleInstallAfter() 0 4 1
A hookModuleUninstallAfter() 0 4 1
A getShippingMethods() 0 18 3
A calculate() 0 4 1
A getModel() 0 6 1
1
<?php
2
3
/**
4
 * @package Shippo
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\shippo;
11
12
use gplcart\core\Container;
13
use gplcart\core\Library;
14
use gplcart\core\Module;
15
16
/**
17
 * Main class for Shippo module
18
 */
19
class Main
20
{
21
22
    /**
23
     * Module class instance
24
     * @var \gplcart\core\Module $module
25
     */
26
    protected $module;
27
28
    /**
29
     * Library class instance
30
     * @var \gplcart\core\Library $library
31
     */
32
    protected $library;
33
34
    /**
35
     * @param Module $module
36
     * @param Library $library
37
     */
38
    public function __construct(Module $module, Library $library)
39
    {
40
        $this->module = $module;
41
        $this->library = $library;
42
    }
43
44
    /**
45
     * Implements hook "library.list"
46
     * @param array $libraries
47
     */
48
    public function hookLibraryList(array &$libraries)
49
    {
50
        $libraries['shippo'] = array(
51
            'name' => 'Shippo', // @text
52
            'description' => 'Shipping API PHP library (USPS, FedEx, UPS and more)', // @text
53
            'url' => 'https://github.com/goshippo/shippo-php-client',
54
            'download' => 'https://github.com/goshippo/shippo-php-client/archive/v1.3.2.zip',
55
            'type' => 'php',
56
            'version_source' => array(
57
                'lines' => 2,
58
                'pattern' => '/.*(\\d+\\.+\\d+\\.+\\d+)/',
59
                'file' => 'vendor/shippo/shippo-php/VERSION'
60
            ),
61
            'module' => 'shippo',
62
            'files' => array(
63
                'vendor/autoload.php'
64
            )
65
        );
66
    }
67
68
    /**
69
     * Implements hook "route.list"
70
     * @param array $routes
71
     */
72
    public function hookRouteList(array &$routes)
73
    {
74
        $routes['admin/module/settings/shippo'] = array(
75
            'access' => 'module_edit',
76
            'handlers' => array(
77
                'controller' => array('gplcart\\modules\\shippo\\controllers\\Settings', 'editSettings')
78
            )
79
        );
80
81
        $routes['admin/tool/shippo'] = array(
82
            'access' => 'shippo_label',
83
            'menu' => array(
84
                'admin' => 'Shipping labels' // @text
85
            ),
86
            'handlers' => array(
87
                'controller' => array('gplcart\\modules\\shippo\\controllers\\Label', 'listLabel')
88
            )
89
        );
90
    }
91
92
    /**
93
     * Implements hook "user.role.permissions"
94
     * @param array $permissions
95
     */
96
    public function hookUserRolePermissions(array &$permissions)
97
    {
98
        $permissions['shippo_label'] = 'Shippo: view and buy labels'; // @text
99
    }
100
101
    /**
102
     * Implements hook "order.calculate.before"
103
     * @param mixed $data
104
     */
105
    public function hookOrderCalculateBefore(array &$data)
106
    {
107
        if (!empty($data['request_shipping_methods'])) {
108
            $this->calculate($data);
109
        }
110
    }
111
112
    /**
113
     * Implements hook "order.submit.before"
114
     * @param array $order
115
     * @param array $options
116
     * @param array $result
117
     */
118
    public function hookOrderSubmitBefore(&$order, $options, &$result)
119
    {
120
        $this->getModel()->validate($order, $options, $result);
121
    }
122
123
    /**
124
     * Implements hook "shipping.methods"
125
     * @param mixed $methods
126
     */
127
    public function hookShippingMethods(array &$methods)
128
    {
129
        $methods = array_merge($methods, $this->getShippingMethods());
130
    }
131
132
    /**
133
     * Implements hook "module.enable.after"
134
     */
135
    public function hookModuleEnableAfter()
136
    {
137
        $this->library->clearCache();
138
    }
139
140
    /**
141
     * Implements hook "module.disable.after"
142
     */
143
    public function hookModuleDisableAfter()
144
    {
145
        $this->library->clearCache();
146
    }
147
148
    /**
149
     * Implements hook "module.install.after"
150
     */
151
    public function hookModuleInstallAfter()
152
    {
153
        $this->library->clearCache();
154
    }
155
156
    /**
157
     * Implements hook "module.uninstall.after"
158
     */
159
    public function hookModuleUninstallAfter()
160
    {
161
        $this->library->clearCache();
162
    }
163
164
    /**
165
     * Returns an array of Shippo's shipping methods
166
     * @param bool $only_enabled
167
     * @return array
168
     */
169
    public function getShippingMethods($only_enabled = true)
170
    {
171
        $settings = $this->module->getSettings('shippo');
172
173
        $methods = array();
174
175
        foreach ($this->getModel()->getServiceNames() as $id => $info) {
176
            list($carrier, $service) = $info;
177
            $methods["shippo_$id"] = array(
178
                'dynamic' => true,
179
                'module' => 'shippo',
180
                'status' => $only_enabled ? in_array("shippo_$id", $settings['enabled']) : null,
181
                'title' => gplcart_text('@carrier - @service', array('@carrier' => $carrier, '@service' => $service))
182
            );
183
        }
184
185
        return $methods;
186
    }
187
188
    /**
189
     * Calculate shipping
190
     * @param $data
191
     */
192
    public function calculate(&$data)
193
    {
194
        $this->getModel()->calculate($data);
195
    }
196
197
    /**
198
     * Returns Shippo's model instance
199
     * @return \gplcart\modules\shippo\models\Shippo
200
     */
201
    public function getModel()
202
    {
203
        /** @var \gplcart\modules\shippo\models\Shippo $instance */
204
        $instance = Container::get('gplcart\\modules\\shippo\\models\\Shippo');
205
        return $instance;
206
    }
207
208
}
209