Completed
Push — master ( 7ccc29...f5d520 )
by Iurii
01:31
created

Shippo::setShippingMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 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\Module,
13
    gplcart\core\Config;
14
15
/**
16
 * Main class for Shippo module
17
 */
18
class Shippo extends Module
19
{
20
21
    /**
22
     * @param Config $config
23
     */
24
    public function __construct(Config $config)
25
    {
26
        parent::__construct($config);
27
    }
28
29
    /**
30
     * Implements hook "library.list"
31
     * @param array $libraries
32
     */
33
    public function hookLibraryList(array &$libraries)
34
    {
35
        $libraries['shippo'] = array(
36
            'name' => /* @text */'Shippo',
37
            'description' => /* @text */'Shipping API PHP library (USPS, FedEx, UPS and more)',
38
            'url' => 'https://github.com/goshippo/shippo-php-client',
39
            'download' => 'https://github.com/goshippo/shippo-php-client/archive/v1.3.2.zip',
40
            'type' => 'php',
41
            'version_source' => array(
42
                'lines' => 2,
43
                'pattern' => '/.*(\\d+\\.+\\d+\\.+\\d+)/',
44
                'file' => 'vendor/shippo/shippo-php/VERSION'
45
            ),
46
            'module' => 'shippo',
47
            'files' => array(
48
                'vendor/autoload.php'
49
            )
50
        );
51
    }
52
53
    /**
54
     * Implements hook "route.list"
55
     * @param array $routes
56
     */
57
    public function hookRouteList(array &$routes)
58
    {
59
        $routes['admin/module/settings/shippo'] = array(
60
            'access' => 'module_edit',
61
            'handlers' => array(
62
                'controller' => array('gplcart\\modules\\shippo\\controllers\\Settings', 'editSettings')
63
            )
64
        );
65
66
        $routes['admin/tool/shippo'] = array(
67
            'access' => 'shippo_label',
68
            'menu' => array('admin' => /* @text */'Shipping labels'),
69
            'handlers' => array(
70
                'controller' => array('gplcart\\modules\\shippo\\controllers\\Label', 'listLabel')
71
            )
72
        );
73
    }
74
75
    /**
76
     * Implements hook "user.role.permissions"
77
     * @param array $permissions
78
     */
79
    public function hookUserRolePermissions(array &$permissions)
80
    {
81
        $permissions['shippo_label'] = /* @text */'Shippo: view and buy labels';
82
    }
83
84
    /**
85
     * Implements hook "order.calculate.before"
86
     * @param mixed $data
87
     */
88
    public function hookOrderCalculateBefore(array &$data)
89
    {
90
        $this->getShippoModel()->calculate($data);
91
    }
92
93
    /**
94
     * Implements hook "order.submit.before"
95
     * @param array $order
96
     * @param array $options
97
     * @param array $result
98
     */
99
    public function hookOrderSubmitBefore(&$order, $options, &$result)
100
    {
101
        $this->getShippoModel()->validate($order, $options, $result);
102
    }
103
104
    /**
105
     * Implements hook "shipping.methods"
106
     * @param mixed $methods
107
     */
108
    public function hookShippingMethods(array &$methods)
109
    {
110
        $this->setShippingMethods($methods);
111
    }
112
113
    /**
114
     * Implements hook "module.enable.after"
115
     */
116
    public function hookModuleEnableAfter()
117
    {
118
        $this->getLibrary()->clearCache();
119
    }
120
121
    /**
122
     * Implements hook "module.disable.after"
123
     */
124
    public function hookModuleDisableAfter()
125
    {
126
        $this->getLibrary()->clearCache();
127
    }
128
129
    /**
130
     * Implements hook "module.install.after"
131
     */
132
    public function hookModuleInstallAfter()
133
    {
134
        $this->getLibrary()->clearCache();
135
    }
136
137
    /**
138
     * Implements hook "module.uninstall.after"
139
     */
140
    public function hookModuleUninstallAfter()
141
    {
142
        $this->getLibrary()->clearCache();
143
    }
144
145
    /**
146
     * Implements hook "module.install.before"
147
     * @param mixed $result
148
     */
149
    public function hookModuleInstallBefore(&$result)
150
    {
151
        if (!function_exists('curl_init')) {
152
            $result = $this->getLanguage()->text('CURL library is not enabled');
153
        }
154
    }
155
156
    /**
157
     * Returns Shippo's model instance
158
     * @return \gplcart\modules\shippo\models\Shippo
159
     */
160
    public function getShippoModel()
161
    {
162
        /* @var $model \gplcart\modules\shippo\models\Shippo */
163
        $model = $this->getModel('Shippo', 'shippo');
164
        return $model;
165
    }
166
167
    /**
168
     * Sets module shipping methods
169
     * @param array $methods
170
     */
171
    protected function setShippingMethods(array &$methods)
172
    {
173
        $language = $this->getLanguage();
174
        $settings = $this->config->getFromModule('shippo');
175
176
        foreach ($this->getShippoModel()->getServiceNames() as $id => $info) {
177
            list($carrier, $service) = $info;
178
            $methods["shippo_$id"] = array(
179
                'dynamic' => true,
180
                'module' => 'shippo',
181
                'status' => in_array("shippo_$id", $settings['enabled']),
182
                'title' => $language->text('@carrier - @service', array('@carrier' => $carrier, '@service' => $service))
183
            );
184
        }
185
    }
186
187
}
188