Main   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hookLibraryList() 0 13 1
A hookModuleUninstallAfter() 0 4 1
A hookModuleInstallBefore() 0 8 2
B hookRouteList() 0 33 1
A hookOrderAddBefore() 0 11 3
A hookUserRolePermissions() 0 7 1
A packOrder() 0 4 1
A getPackedBoxes() 0 4 1
A getFitItems() 0 4 1
A getDbScheme() 0 23 1
A getBoxModel() 0 6 1
A getPackerModel() 0 6 1
1
<?php
2
3
/**
4
 * @package Packer
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0-or-later
8
 */
9
10
namespace gplcart\modules\packer;
11
12
use Exception;
13
use gplcart\core\Config;
14
use gplcart\core\Container;
15
16
/**
17
 * Main class for Packer module
18
 */
19
class Main
20
{
21
22
    /**
23
     * Database class instance
24
     * @var \gplcart\core\Database $db
25
     */
26
    protected $db;
27
28
    /**
29
     * Main constructor.
30
     * @param Config $config
31
     */
32
    public function __construct(Config $config)
33
    {
34
        $this->db = $config->getDb();
35
        $this->db->addScheme($this->getDbScheme());
36
    }
37
38
    /**
39
     * Implements hook "library.list"
40
     * @param array $libraries
41
     */
42
    public function hookLibraryList(array &$libraries)
43
    {
44
        $libraries['packer'] = array(
45
            'name' => 'Box packer', // @text
46
            'description' => '4D bin packing / knapsack problem solver', // @text
47
            'url' => 'https://github.com/dvdoug/BoxPacker',
48
            'download' => 'https://github.com/dvdoug/BoxPacker/archive/2.4.3.zip',
49
            'type' => 'php',
50
            'version' => '2.4.3',
51
            'module' => 'packer',
52
            'vendor' => 'dvdoug/boxpacker'
53
        );
54
    }
55
56
    /**
57
     * Implements hook "module.uninstall.after"
58
     */
59
    public function hookModuleUninstallAfter()
60
    {
61
        $this->db->deleteTable('module_packer_boxes');
62
    }
63
64
    /**
65
     * Implements hook "module.install.before"
66
     * @param null|string
67
     */
68
    public function hookModuleInstallBefore(&$result)
69
    {
70
        try {
71
            $this->db->importScheme('module_packer_boxes', $this->getDbScheme());
72
        } catch (Exception $ex) {
73
            $result = $ex->getMessage();
74
        }
75
    }
76
77
    /**
78
     * Implements hook "route.list"
79
     * @param array $routes
80
     */
81
    public function hookRouteList(array &$routes)
82
    {
83
        $routes['admin/settings/box'] = array(
84
            'menu' => array(
85
                'admin' => 'Boxes' // @text
86
            ),
87
            'access' => 'module_packer_box',
88
            'handlers' => array(
89
                'controller' => array('gplcart\\modules\\packer\\controllers\\Box', 'listBox')
90
            )
91
        );
92
93
        $routes['admin/settings/box/add'] = array(
94
            'access' => 'module_packer_box_add',
95
            'handlers' => array(
96
                'controller' => array('gplcart\\modules\\packer\\controllers\\Box', 'editBox')
97
            )
98
        );
99
100
        $routes['admin/settings/box/fit/(\d+)'] = array(
101
            'access' => 'module_packer_box',
102
            'handlers' => array(
103
                'controller' => array('gplcart\\modules\\packer\\controllers\\Box', 'fitBox')
104
            )
105
        );
106
107
        $routes['admin/settings/box/edit/(\d+)'] = array(
108
            'access' => 'module_packer_box_edit',
109
            'handlers' => array(
110
                'controller' => array('gplcart\\modules\\packer\\controllers\\Box', 'editBox')
111
            )
112
        );
113
    }
114
115
    /**
116
     * Implements hook "order.add.before"
117
     * @param array $data
118
     */
119
    public function hookOrderAddBefore(array &$data)
120
    {
121
        if (!empty($data['cart']['items'])) {
122
123
            try {
124
                $data['data']['packages'] = $this->packOrder($data, $data['cart']);
125
            } catch (Exception $ex) {
126
                trigger_error($ex->getMessage());
127
            }
128
        }
129
    }
130
131
    /**
132
     * Implements hook "user.role.permissions"
133
     * @param array $permissions
134
     */
135
    public function hookUserRolePermissions(array &$permissions)
136
    {
137
        $permissions['module_packer_box'] = 'Packer: access boxes'; // @text
138
        $permissions['module_packer_box_add'] = 'Packer: add box'; // @text
139
        $permissions['module_packer_box_edit'] = 'Packer: edit box'; // @text
140
        $permissions['module_packer_box_delete'] = 'Packer: delete box'; // @text
141
    }
142
143
144
    /**
145
     * Returns an array of boxes needed to fit all the order items
146
     * @param array $order
147
     * @param array $cart
148
     * @return array
149
     */
150
    public function packOrder(array $order, array $cart)
151
    {
152
        return $this->getPackerModel()->packOrder($order, $cart);
153
    }
154
155
    /**
156
     * Pack an array of items into a given array of boxes
157
     * @param array $items
158
     * @param array $boxes
159
     * @return array
160
     */
161
    public function getPackedBoxes(array $items, array $boxes)
162
    {
163
        return $this->getPackerModel()->getPackedBoxes($items, $boxes);
164
    }
165
166
    /**
167
     * Returns an array of items that fit into the box
168
     * @param array $items
169
     * @param array $box
170
     * @return array
171
     */
172
    public function getFitItems(array $items, array $box)
173
    {
174
        return $this->getPackerModel()->getFitItems($items, $box);
175
    }
176
177
    /**
178
     * Returns an array of database scheme
179
     * @return array
180
     */
181
    public function getDbScheme()
182
    {
183
        return array(
184
            'module_packer_boxes' => array(
185
                'fields' => array(
186
                    'name' => array('type' => 'varchar', 'length' => 255, 'not_null' => true),
187
                    'outer_width' => array('type' => 'int', 'length' => 10, 'not_null' => true),
188
                    'outer_length' => array('type' => 'int', 'length' => 10, 'not_null' => true),
189
                    'outer_depth' => array('type' => 'int', 'length' => 10, 'not_null' => true),
190
                    'empty_weight' => array('type' => 'int', 'length' => 10, 'not_null' => true),
191
                    'inner_width' => array('type' => 'int', 'length' => 10, 'not_null' => true),
192
                    'inner_length' => array('type' => 'int', 'length' => 10, 'not_null' => true),
193
                    'inner_depth' => array('type' => 'int', 'length' => 10, 'not_null' => true),
194
                    'max_weight' => array('type' => 'int', 'length' => 10, 'not_null' => true),
195
                    'status' => array('type' => 'int', 'length' => 1, 'not_null' => true, 'default' => 0),
196
                    'weight_unit' => array('type' => 'varchar', 'length' => 255, 'not_null' => true),
197
                    'size_unit' => array('type' => 'varchar', 'length' => 255, 'not_null' => true),
198
                    'box_id' => array('type' => 'int', 'length' => 10, 'auto_increment' => true, 'primary' => true),
199
                    'shipping_method' => array('type' => 'varchar', 'length' => 255, 'not_null' => true, 'default' => '')
200
                )
201
            )
202
        );
203
    }
204
205
    /**
206
     * Returns the Box model instance
207
     * @return \gplcart\modules\packer\models\Box
208
     */
209
    public function getBoxModel()
210
    {
211
        /** @var \gplcart\modules\packer\models\Box $instance */
212
        $instance = Container::get('gplcart\\modules\\packer\\models\\Box');
213
        return $instance;
214
    }
215
216
    /**
217
     * Returns the Packer model instance
218
     * @return \gplcart\modules\packer\models\Packer
219
     */
220
    public function getPackerModel()
221
    {
222
        /** @var \gplcart\modules\packer\models\Packer $instance */
223
        $instance = Container::get('gplcart\\modules\\packer\\models\\Packer');
224
        return $instance;
225
    }
226
}
227