Completed
Push — master ( d942bb...bdf8d4 )
by Michael
63:24 queued 38:29
created

BaseRepository::findMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace Napp\Core\Dbal\Repository;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Napp\Core\Dbal\Criteria\CriteriaCollectionInterface;
9
10
abstract class BaseRepository
11
{
12
    /**
13
     * @var Model
14
     */
15
    protected $query;
16
17
    /**
18
     * @param int $id
19
     * @param array $relations
20
     * @return Model|Collection|null
21
     */
22
    public function find(int $id, array $relations = [])
23
    {
24
        return $this->query->newQuery()->with($relations)->find($id);
25
    }
26
27
    /**
28
     * @param array  $ids
29
     * @param array $relations
30
     * @param array  $columns
31
     * @return Collection
32
     */
33
    public function findMany(array $ids, array $relations = [], $columns = ['*'])
34
    {
35
        return $this->query->newQuery()->with($relations)->findMany($ids, $columns);
36
    }
37
38
    /**
39
     * @param string $attribute
40
     * @param mixed $value
41
     * @return Model|null
42
     */
43
    public function findByAttribute(string $attribute, $value)
44
    {
45
        return $this->query->newQuery()->where($attribute, $value)->first();
46
    }
47
48
    /**
49
     * @param array $attributes
50
     * @return Model|null
51
     */
52
    public function findByAttributes(array $attributes)
53
    {
54
        return $this->query->newQuery()->where($attributes)->first();
55
    }
56
57
    /**
58
     * @param CriteriaCollectionInterface $criteriaCollection
59
     * @return Model|null
60
     */
61
    public function findMatchingCriteria(CriteriaCollectionInterface $criteriaCollection)
62
    {
63
        $query = $this->getCriteriaQuery($criteriaCollection);
64
65
        return $query->first();
66
    }
67
68
    /**
69
     * @param array $relations
70
     * @return Collection
71
     */
72
    public function getAll(array $relations = []): Collection
73
    {
74
        return $this->query->newQuery()->with($relations)->get();
75
    }
76
77
    /**
78
     * @param string $attribute
79
     * @param array|string $value
80
     * @param array $relations
81
     * @return Collection
82
     */
83
    public function getAllByAttribute(string $attribute, $value, array $relations = []): Collection
84
    {
85
        $query = $this->query->newQuery()->with($relations);
86
87
        if (true === is_array($value)) {
88
            return $query->whereIn($attribute, $value)->get();
89
        }
90
91
        return $query->where($attribute, $value)->get();
92
    }
93
94
    /**
95
     * @param array $attributes
96
     * @param array $relations
97
     * @return Collection
98
     */
99
    public function getAllByAttributes(array $attributes, array $relations = []): Collection
100
    {
101
        return $this->query->newQuery()->with($relations)->where($attributes)->get();
102
    }
103
104
    /**
105
     * @param CriteriaCollectionInterface $criteriaCollection
106
     * @return \Illuminate\Database\Eloquent\Builder
107
     */
108
    protected function getCriteriaQuery(CriteriaCollectionInterface $criteriaCollection): Builder
109
    {
110
        $query = $this->query->newQuery();
111
        $query->getQuery()->select($this->query->getTable() . '.*');
112
113
        foreach ($criteriaCollection->getAll() as $criterion) {
114
            $criterion->apply($query);
115
        }
116
117
        return $query;
118
    }
119
120
    /**
121
     * @param CriteriaCollectionInterface $criteriaCollection
122
     * @return Collection
123
     */
124
    public function getAllMatchingCriteria(CriteriaCollectionInterface $criteriaCollection): Collection
125
    {
126
        $query = $this->getCriteriaQuery($criteriaCollection);
127
128
        return $query->get();
129
    }
130
131
    /**
132
     * @param callable $callback
133
     * @param int $attempts
134
     * @return mixed
135
     * @throws \Throwable
136
     */
137
    public function transaction(callable $callback, $attempts = 1)
138
    {
139
        return $this->query->newQuery()->getConnection()->transaction($callback, $attempts);
140
    }
141
142
}
143