These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Gaufrette\Adapter; |
||
4 | |||
5 | use Gaufrette\Adapter; |
||
6 | use Gaufrette\Util; |
||
7 | use Doctrine\DBAL\Connection; |
||
8 | |||
9 | /** |
||
10 | * Doctrine DBAL adapter. |
||
11 | * |
||
12 | * @author Markus Bachmann <[email protected]> |
||
13 | * @author Antoine Hérault <[email protected]> |
||
14 | * @author Leszek Prabucki <[email protected]> |
||
15 | */ |
||
16 | class DoctrineDbal implements Adapter, |
||
17 | ChecksumCalculator, |
||
18 | ListKeysAware |
||
19 | { |
||
20 | protected $connection; |
||
21 | protected $table; |
||
22 | protected $columns = array( |
||
23 | 'key' => 'key', |
||
24 | 'content' => 'content', |
||
25 | 'mtime' => 'mtime', |
||
26 | 'checksum' => 'checksum', |
||
27 | ); |
||
28 | |||
29 | /** |
||
30 | * @param Connection $connection The DBAL connection |
||
31 | * @param string $table The files table |
||
32 | * @param array $columns The column names |
||
33 | */ |
||
34 | public function __construct(Connection $connection, $table, array $columns = array()) |
||
35 | { |
||
36 | $this->connection = $connection; |
||
37 | $this->table = $table; |
||
38 | $this->columns = array_replace($this->columns, $columns); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | View Code Duplication | public function keys() |
|
0 ignored issues
–
show
|
|||
45 | { |
||
46 | $keys = array(); |
||
0 ignored issues
–
show
$keys is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
47 | $stmt = $this->connection->executeQuery(sprintf( |
||
48 | 'SELECT %s FROM %s', |
||
49 | $this->getQuotedColumn('key'), |
||
50 | $this->getQuotedTable() |
||
51 | )); |
||
52 | |||
53 | return $stmt->fetchAll(\PDO::FETCH_COLUMN); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function rename($sourceKey, $targetKey) |
||
60 | { |
||
61 | return (boolean) $this->connection->update( |
||
62 | $this->table, |
||
63 | array($this->getQuotedColumn('key') => $targetKey), |
||
64 | array($this->getQuotedColumn('key') => $sourceKey) |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function mtime($key) |
||
72 | { |
||
73 | return $this->getColumnValue($key, 'mtime'); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | public function checksum($key) |
||
80 | { |
||
81 | return $this->getColumnValue($key, 'checksum'); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function exists($key) |
||
88 | { |
||
89 | return (boolean) $this->connection->fetchColumn( |
||
90 | sprintf( |
||
91 | 'SELECT COUNT(%s) FROM %s WHERE %s = :key', |
||
92 | $this->getQuotedColumn('key'), |
||
93 | $this->getQuotedTable(), |
||
94 | $this->getQuotedColumn('key') |
||
95 | ), |
||
96 | array('key' => $key) |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function read($key) |
||
104 | { |
||
105 | return $this->getColumnValue($key, 'content'); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function delete($key) |
||
112 | { |
||
113 | return (boolean) $this->connection->delete( |
||
114 | $this->table, |
||
115 | array($this->getQuotedColumn('key') => $key) |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function write($key, $content) |
||
123 | { |
||
124 | $values = array( |
||
125 | $this->getQuotedColumn('content') => $content, |
||
126 | $this->getQuotedColumn('mtime') => time(), |
||
127 | $this->getQuotedColumn('checksum') => Util\Checksum::fromContent($content), |
||
128 | ); |
||
129 | |||
130 | if ($this->exists($key)) { |
||
131 | $this->connection->update( |
||
132 | $this->table, |
||
133 | $values, |
||
134 | array($this->getQuotedColumn('key') => $key) |
||
135 | ); |
||
136 | } else { |
||
137 | $values[$this->getQuotedColumn('key')] = $key; |
||
138 | $this->connection->insert($this->table, $values); |
||
139 | } |
||
140 | |||
141 | return Util\Size::fromContent($content); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function isDirectory($key) |
||
148 | { |
||
149 | return false; |
||
150 | } |
||
151 | |||
152 | View Code Duplication | private function getColumnValue($key, $column) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
153 | { |
||
154 | $value = $this->connection->fetchColumn( |
||
155 | sprintf( |
||
156 | 'SELECT %s FROM %s WHERE %s = :key', |
||
157 | $this->getQuotedColumn($column), |
||
158 | $this->getQuotedTable(), |
||
159 | $this->getQuotedColumn('key') |
||
160 | ), |
||
161 | array('key' => $key) |
||
162 | ); |
||
163 | |||
164 | return $value; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | public function listKeys($prefix = '') |
||
171 | { |
||
172 | $prefix = trim($prefix); |
||
173 | |||
174 | $keys = $this->connection->fetchAll( |
||
175 | sprintf( |
||
176 | 'SELECT %s AS _key FROM %s WHERE %s LIKE :pattern', |
||
177 | $this->getQuotedColumn('key'), |
||
178 | $this->getQuotedTable(), |
||
179 | $this->getQuotedColumn('key') |
||
180 | ), |
||
181 | array('pattern' => sprintf('%s%%', $prefix)) |
||
182 | ); |
||
183 | |||
184 | return array( |
||
185 | 'dirs' => array(), |
||
186 | 'keys' => array_map(function ($value) { |
||
187 | return $value['_key']; |
||
188 | }, |
||
189 | $keys), |
||
190 | ); |
||
191 | } |
||
192 | |||
193 | private function getQuotedTable() |
||
194 | { |
||
195 | return $this->connection->quoteIdentifier($this->table); |
||
196 | } |
||
197 | |||
198 | private function getQuotedColumn($column) |
||
199 | { |
||
200 | return $this->connection->quoteIdentifier($this->columns[$column]); |
||
201 | } |
||
202 | } |
||
203 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.