Completed
Pull Request — master (#435)
by Anton
04:35
created

Table::deleteOne()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Crud;
12
13
use Bluz\Application\Exception\ApplicationException;
14
use Bluz\Application\Exception\NotFoundException;
15
use Bluz\Db;
16
use Bluz\Db\Row;
17
use Bluz\Proxy;
18
19
/**
20
 * Crud Table
21
 *
22
 * @package  Bluz\Crud
23
 * @author   AntonShevchuk
24
 * @link     https://github.com/bluzphp/framework/wiki/Crud-Table
25
 */
26
class Table extends AbstractCrud
27
{
28
    /**
29
     * @var \Bluz\Db\Table instance of Db\Table
30
     */
31
    protected $table;
32
33
    /**
34
     * Setup Table instance
35
     *
36
     * @param  Db\Table $table
37
     *
38
     * @return void
39
     */
40 26
    public function setTable(Db\Table $table)
41
    {
42 26
        $this->table = $table;
43 26
    }
44
45
    /**
46
     * Return table instance for manipulation
47
     *
48
     * @return Db\Table
49
     * @throws ApplicationException
50
     */
51 26
    public function getTable()
52
    {
53 26
        if (!$this->table) {
54
            $this->initTable();
55
        }
56 26
        return $this->table;
57
    }
58
59
    /**
60
     * Init table instance for manipulation
61
     *
62
     * @return void
63
     * @throws ApplicationException
64
     */
65
    protected function initTable()
66
    {
67
        $tableClass = class_namespace(static::class) . '\\Table';
68
69
        // check class initialization
70
        if (!class_exists($tableClass) || !is_subclass_of($tableClass, Db\Table::class)) {
71
            throw new ApplicationException('`Table` class is not exists or not initialized');
72
        }
73
74
        /**
75
         * @var Db\Table $tableClass
76
         */
77
        $this->setTable($tableClass::getInstance());
78
    }
79
80
    /**
81
     * Get primary key
82
     *
83
     * @return array
84
     */
85 17
    public function getPrimaryKey()
86
    {
87 17
        return $this->getTable()->getPrimaryKey();
88
    }
89
90
    /**
91
     * Get record from Db or create new object
92
     *
93
     * @param  mixed $primary
94
     *
95
     * @return Row
96
     * @throws NotFoundException
97
     */
98 3
    public function readOne($primary)
99
    {
100 3
        if (!$primary) {
101 1
            return $this->getTable()::create();
102
        }
103
104 2
        $row = $this->getTable()::findRow($primary);
105
106 2
        if (!$row) {
107 1
            throw new NotFoundException('Record not found');
108
        }
109
110 1
        return $row;
111
    }
112
113
    /**
114
     * Get set of records
115
     *
116
     * @param int   $offset
117
     * @param int   $limit
118
     * @param array $params
119
     * @param int   $total
120
     *
121
     * @return array|int|mixed
122
     * @throws ApplicationException
123
     */
124 1
    public function readSet($offset = 0, $limit = 10, $params = [], &$total = null)
125
    {
126 1
        $select = $this->getTable()::select();
127
128
        // switch statement for DB type
129 1
        $type = Proxy\Db::getOption('connect', 'type');
130
        switch ($type) {
131 1
            case 'mysql':
132 1
                $selectPart = $select->getQueryPart('select');
133 1
                $selectPart = 'SQL_CALC_FOUND_ROWS ' . current($selectPart);
134 1
                $select->select($selectPart);
135 1
                $totalSQL = 'SELECT FOUND_ROWS()';
136 1
                break;
137
            case 'pgsql':
138
            default:
139
                $selectTotal = clone $select;
140
                $selectTotal->select('COUNT(*)');
141
                $totalSQL = $selectTotal->getSql();
142
                break;
143
        }
144
145 1
        $select->setLimit($limit);
146 1
        $select->setOffset($offset);
147
148
        // run queries
149
        // use transaction to avoid errors
150 1
        Proxy\Db::transaction(
151 1
            function () use (&$result, &$total, $select, $totalSQL) {
152 1
                $result = $select->execute();
153
154 1
                if (!is_null($total)) {
155 1
                    $total = Proxy\Db::fetchOne($totalSQL);
156
                }
157 1
            }
158
        );
159
160 1
        return $result;
161
    }
162
163
    /**
164
     * Create item
165
     *
166
     * @param  array $data
167
     *
168
     * @return integer
169
     */
170 1
    public function createOne($data)
171
    {
172 1
        $row = $this->getTable()::create();
173 1
        $row->setFromArray($data);
174 1
        return $row->save();
175
    }
176
177
    /**
178
     * Update item
179
     *
180
     * @param  mixed $primary
181
     * @param  array $data
182
     *
183
     * @return integer
184
     * @throws NotFoundException
185
     */
186 2
    public function updateOne($primary, $data)
187
    {
188 2
        $row = $this->getTable()::findRow($primary);
189
190 2
        if (!$row) {
191 1
            throw new NotFoundException('Record not found');
192
        }
193
194 1
        $row->setFromArray($data);
195 1
        return $row->save();
196
    }
197
198
    /**
199
     * Delete item
200
     *
201
     * @param  mixed $primary
202
     *
203
     * @return integer
204
     * @throws NotFoundException
205
     */
206 2
    public function deleteOne($primary)
207
    {
208 2
        $row = $this->getTable()::findRow($primary);
209
210 2
        if (!$row) {
211 1
            throw new NotFoundException('Record not found');
212
        }
213 1
        return $row->delete();
214
    }
215
}
216