Passed
Pull Request — main (#1)
by Mohammad
02:54
created

ConnectionProxy::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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\Query\Builder as QueryBuilder;
9
use Illuminate\Support\Str;
10
use Psr\SimpleCache\InvalidArgumentException;
11
use Shamaseen\Repository\Utility\Model;
12
13
class ConnectionProxy implements ConnectionInterface
14
{
15
    public Connection $realConnection;
16
    private Model $model;
17
18
    public function __construct(Connection $realConnection, Model $model)
19
    {
20
        $this->realConnection = $realConnection;
21
        $this->model = $model;
22
    }
23
24
    // Proxy methods >>>
25
26
    /**
27
     * @throws InvalidArgumentException
28
     */
29
    public function cacheOrNext($fullQuery, callable $next)
30
    {
31
        if ($this->model->requestCacheEnabled) {
32
            $cache = $this->model->requestCache;
33
34
            // if the key exist then return the cached version
35
            if ($fromCache = $cache->get($fullQuery)) {
36
                return $fromCache;
37
            }
38
39
            $result = $next();
40
41
            $cache->set($fullQuery, $result);
42
43
            return $result;
44
        }
45
46
        return $next();
47
    }
48
49
    // Laravel methods >>>
50
51
    /**
52
     * @throws InvalidArgumentException
53
     */
54
    public function selectOne($query, $bindings = [], $useReadPdo = true)
55
    {
56
        $fullQuery = Str::replaceArray('?', $bindings, $query);
57
58
        return $this->cacheOrNext($fullQuery, function () use ($query, $bindings, $useReadPdo) {
59
            return $this->realConnection->selectOne($query, $bindings, $useReadPdo);
60
        });
61
    }
62
63
    /**
64
     * @throws InvalidArgumentException
65
     */
66
    public function select($query, $bindings = [], $useReadPdo = true)
67
    {
68
        $fullQuery = Str::replaceArray('?', $bindings, $query);
69
70
        return $this->cacheOrNext($fullQuery, function () use ($query, $bindings, $useReadPdo) {
71
            return $this->realConnection->select($query, $bindings, $useReadPdo);
72
        });
73
    }
74
75
    public function table($table, $as = null)
76
    {
77
        return $this->realConnection->table($table, $as);
78
    }
79
80
    public function raw($value)
81
    {
82
        return $this->realConnection->raw($value);
83
    }
84
85
    public function cursor($query, $bindings = [], $useReadPdo = true)
86
    {
87
        return $this->realConnection->cursor($query, $bindings, $useReadPdo);
88
    }
89
90
    public function insert($query, $bindings = [])
91
    {
92
        return $this->realConnection->insert($query, $bindings);
93
    }
94
95
    public function update($query, $bindings = [])
96
    {
97
        return $this->realConnection->update($query, $bindings);
98
    }
99
100
    public function delete($query, $bindings = [])
101
    {
102
        return $this->realConnection->delete($query, $bindings);
103
    }
104
105
    public function statement($query, $bindings = [])
106
    {
107
        return $this->realConnection->statement($query, $bindings);
108
    }
109
110
    public function affectingStatement($query, $bindings = [])
111
    {
112
        return $this->realConnection->affectingStatement($query, $bindings);
113
    }
114
115
    public function unprepared($query)
116
    {
117
        return $this->realConnection->unprepared($query);
118
    }
119
120
    public function prepareBindings(array $bindings)
121
    {
122
        return $this->realConnection->prepareBindings($bindings);
123
    }
124
125
    public function transaction(Closure $callback, $attempts = 1)
126
    {
127
        return $this->realConnection->transaction($callback, $attempts);
128
    }
129
130
    public function beginTransaction()
131
    {
132
        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...
133
    }
134
135
    public function commit()
136
    {
137
        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...
138
    }
139
140
    public function rollBack()
141
    {
142
        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...
143
    }
144
145
    public function transactionLevel()
146
    {
147
        return $this->realConnection->transactionLevel();
148
    }
149
150
    public function pretend(Closure $callback)
151
    {
152
        return $this->realConnection->pretend($callback);
153
    }
154
155
    public function getDatabaseName()
156
    {
157
        return $this->realConnection->getDatabaseName();
158
    }
159
160
    public function __set(string $name, $value): void
161
    {
162
        $this->realConnection->{$name} = $value;
163
    }
164
165
    public function __get(string $name)
166
    {
167
        return $this->realConnection->{$name};
168
    }
169
170
    public function __call(string $name, array $arguments)
171
    {
172
        return $this->realConnection->{$name}($arguments);
173
    }
174
175
    /**
176
     * Get a new query builder instance.
177
     *
178
     * @return QueryBuilder
179
     */
180
    public function query()
181
    {
182
        return new QueryBuilder(
183
            // pass our connection instead of laravel one.
184
            $this,
185
            $this->realConnection->getQueryGrammar(),
186
            $this->realConnection->getPostProcessor()
187
        );
188
    }
189
}
190