Completed
Push — master ( ee7140...70e1f2 )
by Iurii
01:14
created

Main::hookModuleEnableAfter()   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\Module;
14
15
/**
16
 * Main class for Shippo module
17
 */
18
class Main
19
{
20
21
    /**
22
     * Module class instance
23
     * @var \gplcart\core\Module $module
24
     */
25
    protected $module;
26
27
    /**
28
     * @param Module $module
29
     */
30
    public function __construct(Module $module)
31
    {
32
        $this->module = $module;
33
    }
34
35
    /**
36
     * Implements hook "library.list"
37
     * @param array $libraries
38
     */
39
    public function hookLibraryList(array &$libraries)
40
    {
41
        $libraries['shippo'] = array(
42
            'name' => 'Shippo', // @text
43
            'description' => 'Shipping API PHP library (USPS, FedEx, UPS and more)', // @text
44
            'url' => 'https://github.com/goshippo/shippo-php-client',
45
            'download' => 'https://github.com/goshippo/shippo-php-client/archive/v1.3.2.zip',
46
            'type' => 'php',
47
            'version' => '1.3.2',
48
            'module' => 'shippo',
49
            'vendor' => 'shippo/shippo-php'
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(
69
                'admin' => 'Shipping labels' // @text
70
            ),
71
            'handlers' => array(
72
                'controller' => array('gplcart\\modules\\shippo\\controllers\\Label', 'listLabel')
73
            )
74
        );
75
    }
76
77
    /**
78
     * Implements hook "user.role.permissions"
79
     * @param array $permissions
80
     */
81
    public function hookUserRolePermissions(array &$permissions)
82
    {
83
        $permissions['shippo_label'] = 'Shippo: view and buy labels'; // @text
84
    }
85
86
    /**
87
     * Implements hook "order.calculate.before"
88
     * @param mixed $data
89
     */
90
    public function hookOrderCalculateBefore(array &$data)
91
    {
92
        if (!empty($data['request_shipping_methods'])) {
93
            $this->calculate($data);
94
        }
95
    }
96
97
    /**
98
     * Implements hook "order.submit.before"
99
     * @param array $order
100
     * @param array $options
101
     * @param array $result
102
     */
103
    public function hookOrderSubmitBefore(&$order, $options, &$result)
104
    {
105
        $this->getModel()->validate($order, $options, $result);
106
    }
107
108
    /**
109
     * Implements hook "shipping.methods"
110
     * @param mixed $methods
111
     */
112
    public function hookShippingMethods(array &$methods)
113
    {
114
        $methods = array_merge($methods, $this->getShippingMethods());
115
    }
116
117
    /**
118
     * Returns an array of Shippo's shipping methods
119
     * @param bool $only_enabled
120
     * @return array
121
     */
122
    public function getShippingMethods($only_enabled = true)
123
    {
124
        $methods = array();
125
        $settings = $this->module->getSettings('shippo');
126
127
        foreach ($this->getModel()->getServiceNames() as $id => $info) {
128
129
            list($carrier, $service) = $info;
130
131
            $methods["shippo_$id"] = array(
132
                'dynamic' => true,
133
                'module' => 'shippo',
134
                'status' => $only_enabled ? in_array("shippo_$id", $settings['enabled']) : null,
135
                'title' => gplcart_text('@carrier - @service', array('@carrier' => $carrier, '@service' => $service))
136
            );
137
        }
138
139
        return $methods;
140
    }
141
142
    /**
143
     * Calculate shipping
144
     * @param $data
145
     */
146
    public function calculate(&$data)
147
    {
148
        $this->getModel()->calculate($data);
149
    }
150
151
    /**
152
     * Returns Shippo's model instance
153
     * @return \gplcart\modules\shippo\models\Shippo
154
     */
155
    public function getModel()
156
    {
157
        /** @var \gplcart\modules\shippo\models\Shippo $instance */
158
        $instance = Container::get('gplcart\\modules\\shippo\\models\\Shippo');
159
        return $instance;
160
    }
161
162
}
163