ConnectionProxy   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 44
dl 0
loc 182
rs 10
c 3
b 1
f 0
wmc 29

26 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __call() 0 3 1
A query() 0 7 1
A commit() 0 3 1
A cursor() 0 3 1
A selectOne() 0 6 1
A transaction() 0 3 1
A pretend() 0 3 1
A cacheOrNext() 0 22 4
A select() 0 6 1
A getDatabaseName() 0 3 1
A statement() 0 3 1
A raw() 0 3 1
A __get() 0 3 1
A beginTransaction() 0 3 1
A delete() 0 3 1
A affectingStatement() 0 3 1
A prepareBindings() 0 3 1
A table() 0 3 1
A update() 0 3 1
A insert() 0 3 1
A __set() 0 3 1
A rollBack() 0 3 1
A unprepared() 0 3 1
A transactionLevel() 0 3 1
A scalar() 0 3 1
1
<?php
2
3
namespace Shamaseen\Repository\Utility\Models;
4
5
use Closure;
6
use Illuminate\Database\Connection;
7
use Illuminate\Database\ConnectionInterface;
8
use Illuminate\Database\Eloquent\Model as LaravelModel;
9
use Illuminate\Database\Query\Builder as QueryBuilder;
10
use Illuminate\Support\Str;
11
use Psr\SimpleCache\InvalidArgumentException;
12
use Shamaseen\Repository\Utility\Model;
13
14
class ConnectionProxy implements ConnectionInterface
15
{
16
    public Connection $realConnection;
17
18
    public function __construct(Connection $realConnection, private readonly Model|LaravelModel $model)
19
    {
20
        $this->realConnection = $realConnection;
21
    }
22
23
    // Proxy methods >>>
24
25
    /**
26
     * @throws InvalidArgumentException
27
     */
28
    public function cacheOrNext($fullQuery, callable $next)
29
    {
30
        if(config('repository.disable_cache')) {
31
            return $next();
32
        }
33
34
        if ($this->model->requestCacheEnabled) {
35
            $cache = $this->model->requestCache;
36
37
            // if the key exist then return the cached version
38
            if ($fromCache = $cache->get($fullQuery)) {
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

38
            if ($fromCache = $cache->/** @scrutinizer ignore-call */ get($fullQuery)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
                return $fromCache;
40
            }
41
42
            $result = $next();
43
44
            $cache->set($fullQuery, $result);
45
46
            return $result;
47
        }
48
49
        return $next();
50
    }
51
52
    // Laravel methods >>>
53
54
    /**
55
     * @throws InvalidArgumentException
56
     */
57
    public function selectOne($query, $bindings = [], $useReadPdo = true)
58
    {
59
        $fullQuery = Str::replaceArray('?', $bindings, $query);
60
61
        return $this->cacheOrNext($fullQuery, function () use ($query, $bindings, $useReadPdo) {
62
            return $this->realConnection->selectOne($query, $bindings, $useReadPdo);
63
        });
64
    }
65
66
    /**
67
     * @throws InvalidArgumentException
68
     */
69
    public function select($query, $bindings = [], $useReadPdo = true)
70
    {
71
        $fullQuery = Str::replaceArray('?', $bindings, $query);
72
73
        return $this->cacheOrNext($fullQuery, function () use ($query, $bindings, $useReadPdo) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->cacheOrNex...ion(...) { /* ... */ }) also could return the type Illuminate\Database\Eloquent\Collection which is incompatible with the return type mandated by Illuminate\Database\ConnectionInterface::select() of array.
Loading history...
74
            return $this->realConnection->select($query, $bindings, $useReadPdo);
75
        });
76
    }
77
78
    public function table($table, $as = null)
79
    {
80
        return $this->realConnection->table($table, $as);
81
    }
82
83
    public function raw($value)
84
    {
85
        return $this->realConnection->raw($value);
86
    }
87
88
    public function cursor($query, $bindings = [], $useReadPdo = true)
89
    {
90
        return $this->realConnection->cursor($query, $bindings, $useReadPdo);
91
    }
92
93
    public function insert($query, $bindings = [])
94
    {
95
        return $this->realConnection->insert($query, $bindings);
96
    }
97
98
    public function update($query, $bindings = [])
99
    {
100
        return $this->realConnection->update($query, $bindings);
101
    }
102
103
    public function delete($query, $bindings = [])
104
    {
105
        return $this->realConnection->delete($query, $bindings);
106
    }
107
108
    public function statement($query, $bindings = [])
109
    {
110
        return $this->realConnection->statement($query, $bindings);
111
    }
112
113
    public function affectingStatement($query, $bindings = [])
114
    {
115
        return $this->realConnection->affectingStatement($query, $bindings);
116
    }
117
118
    public function unprepared($query)
119
    {
120
        return $this->realConnection->unprepared($query);
121
    }
122
123
    public function prepareBindings(array $bindings)
124
    {
125
        return $this->realConnection->prepareBindings($bindings);
126
    }
127
128
    public function transaction(Closure $callback, $attempts = 1)
129
    {
130
        return $this->realConnection->transaction($callback, $attempts);
131
    }
132
133
    public function beginTransaction()
134
    {
135
        return $this->realConnection->beginTransaction();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->realConnection->beginTransaction() targeting Illuminate\Database\Connection::beginTransaction() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
136
    }
137
138
    public function commit()
139
    {
140
        return $this->realConnection->commit();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->realConnection->commit() targeting Illuminate\Database\Connection::commit() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
141
    }
142
143
    public function rollBack()
144
    {
145
        return $this->realConnection->rollBack();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->realConnection->rollBack() targeting Illuminate\Database\Connection::rollBack() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
146
    }
147
148
    public function transactionLevel()
149
    {
150
        return $this->realConnection->transactionLevel();
151
    }
152
153
    public function pretend(Closure $callback)
154
    {
155
        return $this->realConnection->pretend($callback);
156
    }
157
158
    public function getDatabaseName()
159
    {
160
        return $this->realConnection->getDatabaseName();
161
    }
162
163
    public function __set(string $name, $value): void
164
    {
165
        $this->realConnection->{$name} = $value;
166
    }
167
168
    public function __get(string $name)
169
    {
170
        return $this->realConnection->{$name};
171
    }
172
173
    public function __call(string $name, array $arguments)
174
    {
175
        return $this->realConnection->{$name}(...$arguments);
176
    }
177
178
    /**
179
     * Get a new query builder instance.
180
     *
181
     * @return QueryBuilder
182
     */
183
    public function query()
184
    {
185
        return new QueryBuilder(
186
            // pass our connection instead of laravel one.
187
            $this,
188
            $this->realConnection->getQueryGrammar(),
189
            $this->realConnection->getPostProcessor()
190
        );
191
    }
192
193
    public function scalar($query, $bindings = [], $useReadPdo = true)
194
    {
195
        return $this->scalar($query, $bindings, $useReadPdo);
196
    }
197
}
198