UpdateQueryBuilder::update()   A
last analyzed

Complexity

Conditions 3
Paths 10

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 21
c 1
b 0
f 0
nc 10
nop 8
dl 0
loc 39
rs 9.584

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * The Update 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 \Exception;
13
14
/**
15
 * This class provides the mechanism to build the update Queries.
16
 */
17
class UpdateQueryBuilder extends QueryBuilder
18
{
19
    /**
20
     * It updates the matching rows
21
     *
22
     * @param array $rows
23
     * @return boolean
24
     * @throws Exception
25
     * @author RN Kushwaha <[email protected]>
26
     * @since v0.0.8
27
     */
28
    public function update(
29
      $row,
30
      $table,
31
      $where,
32
      $whereRaw,
33
      $whereIn,
34
      $whereNotIn,
35
      $whereNull,
36
      $whereNotNull
37
    ): bool
38
    {
39
        $wqb   = new WhereQueryBuilder();
40
        $query = "UPDATE ".$table." SET ";
41
        $ar    = [];
42
43
        foreach($row as $key => $val){
44
            $ar[':'.$key] = $val;
45
            $query.= $this->quote($key)." =:".$key.",";
46
        }
47
48
        $query = rtrim($query, ",");
49
50
        try{
51
            $whereQuery = $wqb->buildAllWhereQuery(
52
                                $where,
53
                                $whereRaw,
54
                                $whereIn,
55
                                $whereNotIn,
56
                                $whereNull,
57
                                $whereNotNull
58
                            );
59
            $query.= " ".join(" ", $whereQuery);
60
            $stmt = Connection::get()->prepare($this->queryPrefix($query));
61
            $stmt->execute($ar);
62
        } catch(Exception $e){
63
            throw new Exception($e->getMessage());
64
        }
65
66
        return true;
67
    }
68
}
69