1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NilPortugues\Foundation\Infrastructure\Model\Repository\EloquentMongoDB; |
4
|
|
|
|
5
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields; |
6
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter; |
7
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity; |
8
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\ReadRepository; |
9
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort; |
10
|
|
|
|
11
|
|
|
class EloquentReadRepository extends BaseEloquentRepository implements ReadRepository |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Retrieves an entity by its id. |
15
|
|
|
* |
16
|
|
|
* @param Identity $id |
17
|
|
|
* @param Fields|null $fields |
18
|
|
|
* |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
|
|
public function find(Identity $id, Fields $fields = null) |
22
|
|
|
{ |
23
|
|
|
$model = self::$instance; |
24
|
|
|
$columns = ($fields) ? $fields->get() : ['*']; |
25
|
|
|
|
26
|
|
|
return $model->query()->where($model->getKeyName(), '=', $id->id())->get($columns)->first(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns all instances of the type. |
31
|
|
|
* |
32
|
|
|
* @param Filter|null $filter |
33
|
|
|
* @param Sort|null $sort |
34
|
|
|
* @param Fields|null $fields |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
View Code Duplication |
public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$model = self::$instance; |
41
|
|
|
$query = $model->query(); |
42
|
|
|
$columns = ($fields) ? $fields->get() : ['*']; |
43
|
|
|
|
44
|
|
|
if ($filter) { |
45
|
|
|
EloquentFilter::filter($query, $filter); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($sort) { |
49
|
|
|
EloquentSorter::sort($query, $sort); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $query->get($columns)->toArray(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns all instances of the type meeting $distinctFields values. |
57
|
|
|
* |
58
|
|
|
* @param Fields $distinctFields |
59
|
|
|
* @param Filter|null $filter |
60
|
|
|
* @param Sort|null $sort |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function findByDistinct(Fields $distinctFields, Filter $filter = null, Sort $sort = null) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$model = self::$instance; |
67
|
|
|
$query = $model->query(); |
68
|
|
|
|
69
|
|
|
$columns = (count($fields = $distinctFields->get()) > 0) ? $fields : ['*']; |
70
|
|
|
|
71
|
|
|
if ($filter) { |
72
|
|
|
EloquentFilter::filter($query, $filter); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($sort) { |
76
|
|
|
EloquentSorter::sort($query, $sort); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $query->getQuery()->distinct()->get($columns); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns the total amount of elements in the repository given the restrictions provided by the Filter object. |
84
|
|
|
* |
85
|
|
|
* @param Filter|null $filter |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function count(Filter $filter = null) |
90
|
|
|
{ |
91
|
|
|
// TODO: Implement count() method. |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns whether an entity with the given id exists. |
96
|
|
|
* |
97
|
|
|
* @param $id |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function exists(Identity $id) |
102
|
|
|
{ |
103
|
|
|
// TODO: Implement exists() method. |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.