Test Failed
Pull Request — master (#94)
by
unknown
12:10 queued 07:19
created

PostgresConnection::getDefaultSchemaGrammar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\Postgres;
6
7
use DateTimeInterface;
8
use Doctrine\DBAL\Types\Type;
9
use Illuminate\Database\PostgresConnection as BasePostgresConnection;
10
use Illuminate\Support\Traits\Macroable;
11
use PDO;
12
use Umbrellio\Postgres\Extensions\AbstractExtension;
13
use Umbrellio\Postgres\Extensions\Exceptions\ExtensionInvalidException;
14
use Umbrellio\Postgres\Schema\Builder;
15
use Umbrellio\Postgres\Schema\Types\NumericType;
16
use Umbrellio\Postgres\Schema\Types\TsRangeType;
17
use Umbrellio\Postgres\Schema\Types\TsTzRangeType;
18
19
class PostgresConnection extends BasePostgresConnection
20
{
21
    use Macroable;
22
23
    public $name;
24
25
    private static $extensions = [];
26
27
    private $initialTypes = [
28
        TsRangeType::TYPE_NAME => TsRangeType::class,
29
        TsTzRangeType::TYPE_NAME => TsTzRangeType::class,
30
        NumericType::TYPE_NAME => NumericType::class,
31
    ];
32
33
    /**
34
     * @param AbstractExtension|string $extension
35
     * @codeCoverageIgnore
36
     */
37
    final public static function registerExtension(string $extension): void
38
    {
39
        if (! is_subclass_of($extension, AbstractExtension::class)) {
40
            throw new ExtensionInvalidException(sprintf(
41
                'Class %s must be implemented from %s',
42
                $extension,
43
                AbstractExtension::class
44
            ));
45
        }
46
        self::$extensions[$extension::getName()] = $extension;
47
    }
48
49
    public function getSchemaBuilder()
50 27
    {
51
        if ($this->schemaGrammar === null) {
52 27
            $this->useDefaultSchemaGrammar();
53 27
        }
54
        return new Builder($this);
55 27
    }
56
57
    public function useDefaultPostProcessor(): void
58 29
    {
59
        parent::useDefaultPostProcessor();
60 29
61
        $this->registerExtensions();
62 29
        $this->registerInitialTypes();
63 29
    }
64
65
    public function bindValues($statement, $bindings)
66 27
    {
67
        if ($this->getPdo()->getAttribute(PDO::ATTR_EMULATE_PREPARES)) {
68 27
            foreach ($bindings as $key => $value) {
69 8
                $parameter = is_string($key) ? $key : $key + 1;
70 8
71
                switch (true) {
72
                    case is_bool($value):
73 8
                        $dataType = PDO::PARAM_BOOL;
74 2
                        break;
75 2
76
                    case $value === null:
77 6
                        $dataType = PDO::PARAM_NULL;
78 1
                        break;
79 1
80
                    default:
81
                        $dataType = PDO::PARAM_STR;
82 5
                }
83
84
                $statement->bindValue($parameter, $value, $dataType);
85 8
            }
86
        } else {
87
            parent::bindValues($statement, $bindings);
88 19
        }
89
    }
90
91
    public function prepareBindings(array $bindings)
92 27
    {
93
        if ($this->getPdo()->getAttribute(PDO::ATTR_EMULATE_PREPARES)) {
94 27
            $grammar = $this->getQueryGrammar();
95 8
96
            foreach ($bindings as $key => $value) {
97 8
                if ($value instanceof DateTimeInterface) {
98 8
                    $bindings[$key] = $value->format($grammar->getDateFormat());
99 1
                }
100
            }
101
102
            return $bindings;
103 8
        }
104
105
        return parent::prepareBindings($bindings);
106 19
    }
107
108
    private function registerInitialTypes(): void
109 27
    {
110
        foreach ($this->initialTypes as $type => $typeClass) {
111 27
            if (! Type::hasType($type)) {
112
                Type::addType($type, $typeClass);
113
            }
114 29
        }
115
    }
116 29
117 29
    /**
118 1
     * @codeCoverageIgnore
119
     */
120
    private function registerExtensions(): void
121
    {
122
        collect(self::$extensions)->each(function ($extension) {
0 ignored issues
show
Bug introduced by
self::extensions of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

122
        collect(/** @scrutinizer ignore-type */ self::$extensions)->each(function ($extension) {
Loading history...
123
            /** @var AbstractExtension $extension */
124
            $extension::register();
125
            foreach ($extension::getTypes() as $type => $typeClass) {
126
                if (! Type::hasType($type)) {
127
                    Type::addType($type, $typeClass);
128
                }
129
            }
130
        });
131
    }
132
}
133