User::get()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package API
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3
8
 */
9
10
namespace gplcart\modules\api\models;
11
12
use gplcart\core\Config;
13
use gplcart\core\interfaces\Crud as CrudInterface;
14
15
/**
16
 * Manages basic behaviors and data related API users
17
 */
18
class User implements CrudInterface
19
{
20
    /**
21
     * Database class instance
22
     * @var \gplcart\core\Database $db
23
     */
24
    protected $db;
25
26
    /**
27
     * @param Config $config
28
     */
29
    public function __construct(Config $config)
30
    {
31
        $this->db = $config->getDb();
32
    }
33
34
    /**
35
     * Loads an API user
36
     * @param array|int $condition
37
     * @return array
38
     */
39
    public function get($condition)
40
    {
41
        if (!is_array($condition)) {
42
            $condition = array('api_user_id' => $condition);
43
        }
44
45
        $condition['limit'] = array(0, 1);
46
        $list = (array) $this->getList($condition);
47
48
        return empty($list) ? array() : reset($list);
49
    }
50
51
    /**
52
     * Returns an array of API users or counts them
53
     * @param array $options
54
     * @return array|integer
55
     */
56
    public function getList(array $options = array())
57
    {
58
        $sql = 'SELECT au.*, u.status AS user_status, u.role_id AS user_role_id';
59
60
        if (!empty($options['count'])) {
61
            $sql = 'SELECT COUNT(au.api_user_id)';
62
        }
63
64
        $sql .= ' FROM module_api_user au
65
                  LEFT JOIN user u ON(au.user_id = u.user_id)';
66
67
        $conditions = array();
68
69
        if (isset($options['api_user_id'])) {
70
            $sql .= ' WHERE au.api_user_id=?';
71
            $conditions[] = $options['api_user_id'];
72
        } else {
73
            $sql .= ' WHERE au.api_user_id IS NOT NULL';
74
        }
75
76
        if (isset($options['user_id'])) {
77
            $sql .= ' AND au.user_id=?';
78
            $conditions[] = $options['user_id'];
79
        }
80
81
        if (isset($options['secret'])) {
82
            $sql .= ' AND au.secret=?';
83
            $conditions[] = $options['secret'];
84
        }
85
86
        if (isset($options['status'])) {
87
            $sql .= ' AND au.status=?';
88
            $conditions[] = (int) $options['status'];
89
        }
90
91
        $allowed_order = array('asc', 'desc');
92
        $allowed_sort = array('name', 'api_user_id', 'user_id', 'created', 'modified', 'status');
93
94
        if (isset($options['sort'])
95
            && in_array($options['sort'], $allowed_sort)
96
            && isset($options['order'])
97
            && in_array($options['order'], $allowed_order)) {
98
            $sql .= " ORDER BY au.{$options['sort']} {$options['order']}";
99
        } else {
100
            $sql .= ' ORDER BY au.created DESC';
101
        }
102
103
        if (!empty($options['limit'])) {
104
            $sql .= ' LIMIT ' . implode(',', array_map('intval', $options['limit']));
105
        }
106
107
        if (empty($options['count'])) {
108
            $fetch_options = array('index' => 'api_user_id', 'unserialize' => 'data');
109
            $result = $this->db->fetchAll($sql, $conditions, $fetch_options);
110
        } else {
111
            $result = (int) $this->db->fetchColumn($sql, $conditions);
112
        }
113
114
        return $result;
115
    }
116
117
    /**
118
     * Adds a new API user
119
     * @param array $data
120
     * @return int
121
     */
122
    public function add(array $data)
123
    {
124
        $data['created'] = $data['modified'] = GC_TIME;
125
        return (int) $this->db->insert('module_api_user', $data);
126
    }
127
128
    /**
129
     * Deletes a user
130
     * @param int $id
131
     * @return bool
132
     */
133
    public function delete($id)
134
    {
135
        return (bool) $this->db->delete('module_api_user', array('api_user_id' => $id));
136
    }
137
138
    /**
139
     * Updates a user
140
     * @param int $id
141
     * @param array $data
142
     * @return bool
143
     */
144
    public function update($id, array $data)
145
    {
146
        $data['modified'] = GC_TIME;
147
        return (bool) $this->db->update('module_api_user', $data, array('api_user_id' => $id));
148
    }
149
}
150