Test Failed
Pull Request — master (#94)
by
unknown
05:26
created

PostgresConnection   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 21
eloc 50
c 4
b 1
f 1
dl 0
loc 109
ccs 35
cts 35
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A useDefaultPostProcessor() 0 6 1
A registerExtension() 0 10 2
A getSchemaBuilder() 0 6 2
A bindValues() 0 23 6
A registerExtensions() 0 8 3
A registerInitialTypes() 0 5 3
A prepareBindings() 0 15 4
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