1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the BenGorFile package. |
5
|
|
|
* |
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace BenGorFile\File\Infrastructure\Persistence\Sql; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Beñat Espiña <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class SqlListOfIdsSpecification implements SqlQuerySpecification, SqlCountSpecification |
19
|
|
|
{ |
20
|
|
|
private $ids; |
21
|
|
|
private $offset; |
22
|
|
|
private $limit; |
23
|
|
|
|
24
|
|
|
public function __construct(array $ids, $offset, $limit) |
25
|
|
|
{ |
26
|
|
|
$this->ids = $ids; |
27
|
|
|
$this->offset = $offset; |
28
|
|
|
$this->limit = $limit; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function buildQuery() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$whereClause = $this->buildWhere(); |
34
|
|
|
$limitClause = $this->buildLimit(); |
35
|
|
|
$query = "SELECT * FROM file f $whereClause ORDER BY f.updated_on DESC $limitClause"; |
36
|
|
|
$parameters = $this->buildParameters(); |
37
|
|
|
|
38
|
|
|
return [$query, $parameters]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function buildCount() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$whereClause = $this->buildWhere(); |
44
|
|
|
$limitClause = $this->buildLimit(); |
45
|
|
|
$query = "SELECT COUNT(*) FROM file f $whereClause ORDER BY f.updated_on DESC $limitClause"; |
46
|
|
|
|
47
|
|
|
return [$query, $this->buildParameters()]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function buildWhere() |
51
|
|
|
{ |
52
|
|
|
$whereClause = !empty($this->ids) ? 'WHERE f.id IN (:ids) ' : ''; |
53
|
|
|
|
54
|
|
|
return $whereClause; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function buildLimit() |
58
|
|
|
{ |
59
|
|
|
$limitClause = $this->limit > 0 ? 'LIMIT :limit OFFSET :offset' : ''; |
60
|
|
|
|
61
|
|
|
return $limitClause; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function buildParameters() |
65
|
|
|
{ |
66
|
|
|
$parameters = []; |
67
|
|
|
if (!empty($this->name)) { |
|
|
|
|
68
|
|
|
$parameters = array_merge([':ids' => implode(',', $this->ids)], $parameters); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($this->limit > 0) { |
72
|
|
|
$parameters = array_merge([':limit' => $this->limit, ':offset' => $this->offset], $parameters); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $parameters; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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.