Completed
Push — master ( 003fdd...81f214 )
by Iurii
02:43
created

Main::getTranslationModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 "module.install.before"
94
     * @param mixed $result
95
     */
96
    public function hookModuleInstallBefore(&$result)
97
    {
98
        if (!extension_loaded('curl')) {
99
            $result = gplcart_text('CURL library is not enabled');
100
        }
101
    }
102
103
    /**
104
     * Implements hook "user.role.permissions"
105
     * @param array $permissions
106
     */
107
    public function hookUserRolePermissions(array &$permissions)
108
    {
109
        $permissions['shippo_label'] = 'Shippo: view and buy labels'; // @text
110
    }
111
112
    /**
113
     * Implements hook "order.calculate.before"
114
     * @param mixed $data
115
     */
116
    public function hookOrderCalculateBefore(array &$data)
117
    {
118
        $this->getModel()->calculate($data);
119
    }
120
121
    /**
122
     * Implements hook "order.submit.before"
123
     * @param array $order
124
     * @param array $options
125
     * @param array $result
126
     */
127
    public function hookOrderSubmitBefore(&$order, $options, &$result)
128
    {
129
        $this->getModel()->validate($order, $options, $result);
130
    }
131
132
    /**
133
     * Implements hook "shipping.methods"
134
     * @param mixed $methods
135
     */
136
    public function hookShippingMethods(array &$methods)
137
    {
138
        $this->setShippingMethods($methods);
139
    }
140
141
    /**
142
     * Implements hook "module.enable.after"
143
     */
144
    public function hookModuleEnableAfter()
145
    {
146
        $this->library->clearCache();
147
    }
148
149
    /**
150
     * Implements hook "module.disable.after"
151
     */
152
    public function hookModuleDisableAfter()
153
    {
154
        $this->library->clearCache();
155
    }
156
157
    /**
158
     * Implements hook "module.install.after"
159
     */
160
    public function hookModuleInstallAfter()
161
    {
162
        $this->library->clearCache();
163
    }
164
165
    /**
166
     * Implements hook "module.uninstall.after"
167
     */
168
    public function hookModuleUninstallAfter()
169
    {
170
        $this->library->clearCache();
171
    }
172
173
    /**
174
     * Sets module shipping methods
175
     * @param array $methods
176
     */
177
    protected function setShippingMethods(array &$methods)
178
    {
179
        $settings = $this->module->getSettings('shippo');
180
181
        foreach ($this->getModel()->getServiceNames() as $id => $info) {
182
            list($carrier, $service) = $info;
183
            $methods["shippo_$id"] = array(
184
                'dynamic' => true,
185
                'module' => 'shippo',
186
                'status' => in_array("shippo_$id", $settings['enabled']),
187
                'title' => gplcart_text('@carrier - @service', array('@carrier' => $carrier, '@service' => $service))
188
            );
189
        }
190
    }
191
192
    /**
193
     * Returns Shippo's model instance
194
     * @return \gplcart\modules\shippo\models\Shippo
195
     */
196
    public function getModel()
197
    {
198
        /** @var \gplcart\modules\shippo\models\Shippo $instance */
199
        $instance = Container::get('gplcart\\modules\\shippo\\models\\Shippo');
200
        return $instance;
201
    }
202
203
}
204