Passed
Push — master ( f939d9...4d81c4 )
by
unknown
03:17
created

EloquentRepository::doInsert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zebrainsteam\LaravelRepos;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
use Illuminate\Support\Facades\DB;
10
use InvalidArgumentException;
11
use Repositories\Core\AbstractRepository;
12
13
class EloquentRepository extends AbstractRepository
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $modelClass;
19
20
    public function __construct(string $modelClass)
21
    {
22
        if (!class_exists($modelClass)) {
23
            throw new \InvalidArgumentException("Class " . $modelClass . " doesn't exist");
24
        }
25
        $model = new $modelClass();
26
        if (!$model instanceof Model) {
27
            throw new \InvalidArgumentException("Class " . $modelClass . " must be inherited from " . Model::class);
28
        }
29
30
        $this->modelClass = $modelClass;
31
    }
32
33
    /**
34
     * @param $params
35
     * @return iterable|null
36
     */
37
    protected function doGet($params): ?iterable
38
    {
39
        return $this->modelClass::where($params)->get();
40
    }
41
42
    /**
43
     * @param array $filter
44
     * @return Model|object|Builder|null
45
     */
46
    protected function doFirst(array $filter)
47
    {
48
        return $this->modelClass::where($filter)->first();
49
    }
50
51
    /**
52
     * @param int $id
53
     * @param array|null $select
54
     * @return Model|Collection|Builder[]|Builder|null
55
     */
56
    protected function doGetById(int $id, array $select = null)
57
    {
58
        return $this->modelClass::find($id, is_null($select) ? ['*'] : $select);
59
    }
60
61
    /**
62
     * @param int $id
63
     * @param array|null $select
64
     * @return Model|Collection|Builder|Builder[]
65
     *
66
     * @throws ModelNotFoundException
67
     */
68
    protected function doGetByIdOrFail(int $id, array $select = null)
69
    {
70
        return $this->modelClass::findOrFail($id, is_null($select) ? ['*'] : $select);
71
    }
72
73
    /**
74
     * @param array $data
75
     * @return Model|Builder
76
     */
77
    protected function doCreate(array $data)
78
    {
79
        return $this->modelClass::create($data);
80
    }
81
82
    /**
83
     * @param iterable $data
84
     */
85
    protected function doInsert(iterable $data): void
86
    {
87
        foreach ($data as $item) {
88
            $this->modelClass::create($item);
89
        }
90
    }
91
92
    /**
93
     * @param $model
94
     * @param array $data
95
     *
96
     * @throws ModelNotFoundException
97
     */
98
    protected function doUpdate($model, array $data): void
99
    {
100
        $this->validateModel($model);
101
        $model->update($data);
102
    }
103
104
    /**
105
     * @param $model
106
     *
107
     * *@throws ModelNotFoundException
108
     */
109
    protected function doDelete($model): void
110
    {
111
        $this->validateModel($model);
112
        $model->delete();
113
    }
114
115
    /**
116
     * @param array $filter
117
     * @return bool
118
     */
119
    protected function doExists(array $filter): bool
120
    {
121
        return !empty($this->first($filter));
122
    }
123
124
    /**
125
     * @param array $filter
126
     * @return int
127
     */
128
    protected function doCount(array $filter = []): int
129
    {
130
        return $this->modelClass::where($filter)->count();
131
    }
132
133
    /**
134
     * @return void
135
     */
136
    protected function doOpenTransaction(): void
137
    {
138
        DB::beginTransaction();
139
    }
140
141
    /**
142
     * @return void
143
     */
144
    protected function doCommitTransaction(): void
145
    {
146
        DB::commit();
147
    }
148
149
    /**
150
     * @return void
151
     */
152
    protected function doRollbackTransaction(): void
153
    {
154
        DB::rollBack();
155
    }
156
157
    /**
158
     * @param $model
159
     * @return void
160
     *
161
     * @throws InvalidArgumentException
162
     */
163
    private function validateModel($model): void
164
    {
165
        $modelClass = new $this->modelClass();
166
        if (!$model instanceof $modelClass) {
167
            throw new \InvalidArgumentException("The passed argument must be an instance of " . $this->modelClass);
168
        }
169
    }
170
}
171