DbHelper::getAll()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 2
eloc 9
nc 2
nop 3
1
<?php
2
3
namespace Mouf\Database\QueryWriter\Utils;
4
5
use Mouf\MoufManager;
6
7
class DbHelper
8
{
9
    public static function getAll($sql, $offset, $limit)
10
    {
11
        $dbalConnection = MoufManager::getMoufManager()->get('dbalConnection');
12
        /* @var $dbalConnection \Doctrine\DBAL\Connection */
13
        $sql .= self::getFromLimitString($offset, $limit);
14
        $statement = $dbalConnection->executeQuery($sql);
15
        $results = $statement->fetchAll();
16
17
        $array = [];
18
19
        foreach ($results as $result) {
20
            $array[] = $result;
21
        }
22
23
        return $array;
24
    }
25
26
    public static function getFromLimitString($from = null, $limit = null)
27
    {
28
        if ($limit !== null) {
29
            $limitInt = (int) $limit;
30
            $queryStr = ' LIMIT '.$limitInt;
31
32
            if ($from !== null) {
33
                $fromInt = (int) $from;
34
                $queryStr .= ' OFFSET '.$fromInt;
35
            }
36
37
            return $queryStr;
38
        } else {
39
            return '';
40
        }
41
    }
42
}
43