Completed
Pull Request — master (#247)
by
unknown
09:38
created

Crud::readSet()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 20
c 0
b 0
f 0
nc 8
nop 3
dl 0
loc 32
rs 8.439
ccs 18
cts 18
cp 1
crap 5
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Test;
11
12
use Bluz\Proxy\Db;
13
use Bluz\Proxy\Request;
14
use Bluz\Proxy\Response;
15
16
/**
17
 * Crud based on Db\Table
18
 *
19
 * @package  Application\Test
20
 *
21
 * @method   Table getTable()
22
 *
23
 * @author   Anton Shevchuk
24
 * @created  03.09.12 13:11
25
 */
26
class Crud extends \Bluz\Crud\Table
27
{
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @param int $offset
32
     * @param int $limit
33
     * @param array $params
34
     * @return array|int|mixed
35
     */
36 1
    public function readSet($offset = 0, $limit = 10, $params = array())
37
    {
38 1
        $select = Db::select('*')
39 1
            ->from('test', 't');
40
41 1
        if ($limit) {
42 1
            $selectPart = $select->getQueryPart('select');
43 1
            $selectPart = 'SQL_CALC_FOUND_ROWS ' . current($selectPart);
44 1
            $select->select($selectPart);
45
46 1
            $select->setLimit($limit);
47 1
            $select->setOffset($offset);
48
        }
49
50 1
        $result = $select->execute('\\Application\\Test\\Row');
51
52 1
        if ($limit) {
53 1
            $total = Db::fetchOne('SELECT FOUND_ROWS()');
54
        } else {
55
            $total = sizeof($result);
56
        }
57
58 1
        if (sizeof($result) < $total && Request::METHOD_GET == Request::getMethod()) {
59 1
            Response::setStatusCode(206);
60 1
            Response::setHeader(
61 1
                'Content-Range',
62 1
                'items ' . $offset . '-' . ($offset + sizeof($result)) . '/' . $total
63
            );
64
        }
65
66 1
        return $result;
67
    }
68
}
69