Completed
Push — master ( f5d520...cbec7f )
by Iurii
01:36
created

Shippo::getModel()   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\Module,
13
    gplcart\core\Library,
14
    gplcart\core\Container;
15
16
/**
17
 * Main class for Shippo module
18
 */
19
class Shippo
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' => /* @text */'Shippo',
52
            'description' => /* @text */'Shipping API PHP library (USPS, FedEx, UPS and more)',
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('admin' => /* @text */'Shipping labels'),
84
            'handlers' => array(
85
                'controller' => array('gplcart\\modules\\shippo\\controllers\\Label', 'listLabel')
86
            )
87
        );
88
    }
89
90
    /**
91
     * Implements hook "module.install.before"
92
     * @param mixed $result
93
     */
94
    public function hookModuleInstallBefore(&$result)
95
    {
96
        if (!extension_loaded('curl')) {
97
            $result = $this->getLanguage()->text('CURL library is not enabled');
98
        }
99
    }
100
101
    /**
102
     * Implements hook "user.role.permissions"
103
     * @param array $permissions
104
     */
105
    public function hookUserRolePermissions(array &$permissions)
106
    {
107
        $permissions['shippo_label'] = /* @text */'Shippo: view and buy labels';
108
    }
109
110
    /**
111
     * Implements hook "order.calculate.before"
112
     * @param mixed $data
113
     */
114
    public function hookOrderCalculateBefore(array &$data)
115
    {
116
        $this->getModel()->calculate($data);
117
    }
118
119
    /**
120
     * Implements hook "order.submit.before"
121
     * @param array $order
122
     * @param array $options
123
     * @param array $result
124
     */
125
    public function hookOrderSubmitBefore(&$order, $options, &$result)
126
    {
127
        $this->getModel()->validate($order, $options, $result);
128
    }
129
130
    /**
131
     * Implements hook "shipping.methods"
132
     * @param mixed $methods
133
     */
134
    public function hookShippingMethods(array &$methods)
135
    {
136
        $this->setShippingMethods($methods);
137
    }
138
139
    /**
140
     * Implements hook "module.enable.after"
141
     */
142
    public function hookModuleEnableAfter()
143
    {
144
        $this->library->clearCache();
145
    }
146
147
    /**
148
     * Implements hook "module.disable.after"
149
     */
150
    public function hookModuleDisableAfter()
151
    {
152
        $this->library->clearCache();
153
    }
154
155
    /**
156
     * Implements hook "module.install.after"
157
     */
158
    public function hookModuleInstallAfter()
159
    {
160
        $this->library->clearCache();
161
    }
162
163
    /**
164
     * Implements hook "module.uninstall.after"
165
     */
166
    public function hookModuleUninstallAfter()
167
    {
168
        $this->library->clearCache();
169
    }
170
171
    /**
172
     * Sets module shipping methods
173
     * @param array $methods
174
     */
175
    protected function setShippingMethods(array &$methods)
176
    {
177
        $settings = $this->module->getSettings('shippo');
178
179
        foreach ($this->getModel()->getServiceNames() as $id => $info) {
180
            list($carrier, $service) = $info;
181
            $methods["shippo_$id"] = array(
182
                'dynamic' => true,
183
                'module' => 'shippo',
184
                'status' => in_array("shippo_$id", $settings['enabled']),
185
                'title' => $this->getLanguage()->text('@carrier - @service', array('@carrier' => $carrier, '@service' => $service))
186
            );
187
        }
188
    }
189
190
    /**
191
     * Returns Shippo's model instance
192
     * @return \gplcart\modules\shippo\models\Shippo
193
     */
194
    public function getModel()
195
    {
196
        return Container::get('gplcart\\modules\\shippo\\models\\Shippo');
197
    }
198
199
    /**
200
     * Language model class instance
201
     * @return \gplcart\core\models\Language
202
     */
203
    protected function getLanguage()
204
    {
205
        return Container::get('gplcart\\core\\models\\Language');
206
    }
207
208
}
209