Passed
Push — master ( 357beb...8239d1 )
by Rougin
02:56
created

Builder::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0185
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 32
            $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 3
                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 3
    public function set($key, $value)
77
    {
78 3
        $parameters = $this->getParameters();
79
80 3
        $index = count((array) $parameters);
81
82 3
        $this->setParameter($index, $value);
83
84 3
        return $this->add('set', $key . ' = ?', true);
85
    }
86
87 3
    public function values(array $values)
88
    {
89 3
        $index = 0;
90
91 3
        foreach ($values as $key => $value)
92
        {
93 3
            $this->setParameter($index, $value);
94
95 3
            $index = $index + 1;
96
97 3
            $values[$key] = '?';
98 2
        }
99
100 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

100
        return $this->add('values', /** @scrutinizer ignore-type */ $values);
Loading history...
101
    }
102
}
103