Passed
Push — master ( 8239d1...9db5ce )
by Rougin
01:48
created

Builder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 10
eloc 33
dl 0
loc 96
ccs 38
cts 40
cp 0.95
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSql() 0 29 5
A __construct() 0 8 2
A values() 0 14 2
A set() 0 9 1
1
<?php
2
3
namespace Rougin\Windstorm\Doctrine;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Query\QueryBuilder;
7
use Rougin\Windstorm\Doctrine\Builder\DeleteQuery;
8
use Rougin\Windstorm\Doctrine\Builder\InsertQuery;
9
use Rougin\Windstorm\Doctrine\Builder\SelectQuery;
10
use Rougin\Windstorm\Doctrine\Builder\UpdateQuery;
11
12
/**
13
 * Query Builder
14
 *
15
 * @package Windstorm
16
 * @author  Rougin Gutib <[email protected]>
17
 */
18
class Builder extends QueryBuilder
19
{
20
    /**
21
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
22
     */
23
    protected $platform;
24
25
    /**
26
     * Initializes the platform instance.
27
     *
28
     * @param \Doctrine\DBAL\Connection|\Doctrine\DBAL\Platforms\AbstractPlatform $platform
29
     */
30 108
    public function __construct($platform)
31
    {
32 108
        if ($platform instanceof Connection)
33 72
        {
34
            $platform = $platform->getDatabasePlatform();
35
        }
36
37 108
        $this->platform = $platform;
38 108
    }
39
40
    /**
41
     * Returns the complete SQL string.
42
     *
43
     * @return string
44
     */
45 102
    public function getSql()
46
    {
47 102
        $first = $this->getFirstResult();
48
49 102
        $max = $this->getMaxResults();
50
51 102
        $parts = $this->getQueryParts();
52
53 102
        switch ($this->getType()) {
54 102
            case self::INSERT:
55 3
                $sql = new InsertQuery($parts);
56
57 3
                break;
58 99
            case self::DELETE:
59 6
                $sql = new DeleteQuery($parts);
60
61 6
                break;
62 93
            case self::UPDATE:
63 3
                $sql = new UpdateQuery($parts);
64
65 35
                break;
66 90
            case self::SELECT:
67 60
            default:
68 90
                $sql = new SelectQuery($parts, $this->platform, $max, $first);
69
70 90
                break;
71 68
        }
72
73 102
        return (string) $sql->get();
74
    }
75
76
    /**
77
     * Sets a new value for a column in a bulk update query.
78
     *
79
     * @param  string $key
80
     * @param  mixed  $value
81
     * @return self
82
     */
83 3
    public function set($key, $value)
84
    {
85 3
        $parameters = $this->getParameters();
86
87 3
        $index = count((array) $parameters);
88
89 3
        $this->setParameter($index, $value);
90
91 3
        return $this->add('set', $key . ' = ?', true);
92
    }
93
94
    /**
95
     * Specifies values for an insert query indexed by column names.
96
     *
97
     * @param  array  $values
98
     * @return self
99
     */
100 3
    public function values(array $values)
101
    {
102 3
        $index = 0;
103
104 3
        foreach ($values as $key => $value)
105
        {
106 3
            $this->setParameter($index, $value);
107
108 3
            $index = $index + 1;
109
110 3
            $values[$key] = '?';
111 2
        }
112
113 3
        return $this->add('values', $values);
0 ignored issues
show
Bug introduced by
$values of type array is incompatible with the type string expected by parameter $sqlPart of Doctrine\DBAL\Query\QueryBuilder::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
        return $this->add('values', /** @scrutinizer ignore-type */ $values);
Loading history...
114
    }
115
}
116