Box   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 0
dl 0
loc 148
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 4 1
A add() 0 4 1
A delete() 0 4 1
A update() 0 4 1
C getList() 0 55 11
B getListByShippingMethod() 0 21 5
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\models;
11
12
use gplcart\core\Config;
13
use gplcart\core\interfaces\Crud;
14
15
/**
16
 * Manages basic behaviors and data related to Packer module
17
 */
18
class Box implements Crud
19
{
20
21
    /**
22
     * Database class instance
23
     * @var \gplcart\core\Database $db
24
     */
25
    protected $db;
26
27
    /**
28
     * Box constructor.
29
     * @param Config $config
30
     */
31
    public function __construct(Config $config)
32
    {
33
        $this->db = $config->getDb();
34
    }
35
36
    /**
37
     * Loads an box from the database
38
     * @param int $box_id
39
     * @return array
40
     */
41
    public function get($box_id)
42
    {
43
        return $this->db->fetch('SELECT * FROM module_packer_boxes WHERE box_id=?', array($box_id));
44
    }
45
46
    /**
47
     * Adds a new box
48
     * @param array $data
49
     * @return int
50
     */
51
    public function add(array $data)
52
    {
53
        return $this->db->insert('module_packer_boxes', $data);
54
    }
55
56
    /**
57
     * Deletes a box from the database
58
     * @param int $id
59
     * @return bool
60
     */
61
    public function delete($id)
62
    {
63
        return (bool) $this->db->delete('module_packer_boxes', array('box_id' => $id));
64
    }
65
66
    /**
67
     * Updates a box
68
     * @param int $id
69
     * @param array $data
70
     * @return bool
71
     */
72
    public function update($id, array $data)
73
    {
74
        return (bool) $this->db->update('module_packer_boxes', $data, array('box_id' => $id));
75
    }
76
77
    /**
78
     * Returns an array of boxes or counts them
79
     * @param array $options
80
     * @return array|integer
81
     */
82
    public function getList(array $options = array())
83
    {
84
        $sql = 'SELECT *';
85
86
        if (!empty($options['count'])) {
87
            $sql = 'SELECT COUNT(box_id)';
88
        }
89
90
        $sql .= ' FROM module_packer_boxes WHERE box_id IS NOT NULL';
91
92
        $conditions = array();
93
94
        if (isset($options['name'])) {
95
            $sql .= ' AND name=?';
96
            $conditions[] = $options['name'];
97
        }
98
99
        if (isset($options['status'])) {
100
            $sql .= ' AND status=?';
101
            $conditions[] = (int) $options['status'];
102
        }
103
104
        if (isset($options['shipping_method'])) {
105
            $sql .= ' AND shipping_method = ?';
106
            $conditions[] = $options['shipping_method'];
107
        }
108
109
        $allowed_order = array('asc', 'desc');
110
111
        $allowed_sort = array(
112
            'name', 'status', 'shipping_method', 'outer_width',
113
            'outer_length', 'outer_depth', 'empty_weight', 'inner_width',
114
            'inner_width', 'inner_length', 'inner_depth', 'max_weight',
115
            'weight_unit', 'size_unit'
116
        );
117
118
        if (isset($options['sort'])
119
            && in_array($options['sort'], $allowed_sort)
120
            && isset($options['order'])
121
            && in_array($options['order'], $allowed_order)) {
122
            $sql .= " ORDER BY {$options['sort']} {$options['order']}";
123
        } else {
124
            $sql .= ' ORDER BY box_id DESC';
125
        }
126
127
        if (!empty($options['limit'])) {
128
            $sql .= ' LIMIT ' . implode(',', array_map('intval', $options['limit']));
129
        }
130
131
        if (empty($options['count'])) {
132
            return $this->db->fetchAll($sql, $conditions, array('index' => 'box_id'));
133
        }
134
135
        return (int) $this->db->fetchColumn($sql, $conditions);
136
    }
137
138
    /**
139
     * Returns an array of matching boxes for the shipping method
140
     * @param string $shipping_method
141
     * @param bool $only_enabled
142
     * @return array
143
     */
144
    public function getListByShippingMethod($shipping_method, $only_enabled = true)
145
    {
146
        $boxes = array();
147
148
        foreach ((array) $this->getList(array('status' => $only_enabled ? true : null)) as $box) {
149
150
            if ($box['shipping_method'] === '') {
151
                $boxes[] = $box;
152
                continue;
153
            }
154
155
            // Glob-like matching. Check wildcards "*" and "?"
156
            $pattern = "#^" . strtr(preg_quote($shipping_method, '#'), array('\*' => '.*', '\?' => '.')) . "$#i";
157
158
            if (preg_match($pattern, $box['shipping_method']) === 1) {
159
                $boxes[] = $box;
160
            }
161
        }
162
163
        return $boxes;
164
    }
165
}
166