PrepareQueryBuilder::prepare()   A
last analyzed

Complexity

Conditions 5
Paths 22

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 18
c 1
b 1
f 0
nc 22
nop 4
dl 0
loc 27
rs 9.3554
1
<?php
2
/**
3
 * The Prepare Query builder Class.
4
 *
5
 * @author RN Kushwaha <[email protected]>
6
 * @since v0.0.8 <Date: 29th Dec, 2019>
7
 */
8
9
namespace Dolphin\Builders;
10
11
use Dolphin\Connections\Connection;
12
use Dolphin\Parsers\WhereQueryParser;
13
use Dolphin\Utils\Utils;
14
use \Exception;
15
16
/**
17
 * This class provides the mechanism to build the Queries.
18
 */
19
class PrepareQueryBuilder extends QueryBuilder
20
{
21
    private function checkCountable($results = null ): bool{
22
       return (is_array($results) || is_object($results)) && count($results);
23
    }
24
25
    /**
26
     * It prepared and execute queries
27
     *
28
     * @param array $rows
29
     * @return boolean
30
     * @throws Exception
31
     * @author RN Kushwaha <[email protected]>
32
     * @since v0.0.5
33
     */
34
     public function prepare($where, $className, $query, $fetchRows = 'all')
35
     {
36
         $wqp  = new WhereQueryParser();
37
         $util = new Utils();
38
         $rows = null;
39
40
         try {
41
             $ar = $wqp->parseWhereQuery($where);
42
             $stmt = Connection::get()->prepare($this->queryPrefix($query));
43
             $stmt->execute($ar);
44
45
             if ($fetchRows == 'first') {
46
                 $results = $stmt->fetch(\PDO::FETCH_ASSOC);
47
             } else{
48
                 $results = $stmt->fetchAll(\PDO::FETCH_ASSOC);
49
             }
50
51
             if($this->checkCountable($results) ){
52
               // now turn this stdClass object to the object type of calling model
53
               $rows = $util->turnObjects($className, $results);
54
             }
55
56
             return $rows;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rows also could return the type object which is incompatible with the documented return type boolean.
Loading history...
57
         } catch (\PDOException $ex) {
58
             throw new \PDOException($ex->getMessage(), 1);
59
         } catch (Exception $e) {
60
             throw new Exception($e->getMessage(), 1);
61
         }
62
     }
63
}
64