1 | <?php |
||
2 | |||
3 | namespace App\Repositories; |
||
4 | |||
5 | use Exception; |
||
6 | use Illuminate\Contracts\Auth\Guard; |
||
7 | use Illuminate\Database\Eloquent\Collection; |
||
8 | use Illuminate\Database\Eloquent\Model; |
||
9 | |||
10 | abstract class AbstractRepository implements RepositoryInterface |
||
11 | { |
||
12 | /** @var Model */ |
||
13 | protected $model; |
||
14 | |||
15 | /** @var Guard */ |
||
16 | protected $auth; |
||
17 | |||
18 | abstract public function getModelClass(): string; |
||
19 | |||
20 | 132 | public function __construct() |
|
21 | { |
||
22 | 132 | $this->model = app($this->getModelClass()); |
|
23 | |||
24 | // This instantiation may fail during a console command if e.g. APP_KEY is empty, |
||
25 | // rendering the whole installation failing. |
||
26 | try { |
||
27 | 132 | $this->auth = app(Guard::class); |
|
28 | } catch (Exception $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
29 | } |
||
30 | 132 | } |
|
31 | |||
32 | 13 | public function getOneById($id): ?Model |
|
33 | { |
||
34 | 13 | return $this->model->find($id); |
|
35 | } |
||
36 | |||
37 | 7 | public function getByIds(array $ids): Collection |
|
38 | { |
||
39 | 7 | return $this->model->whereIn($this->model->getKeyName(), $ids)->get(); |
|
40 | } |
||
41 | |||
42 | public function getAll(): Collection |
||
43 | { |
||
44 | return $this->model->all(); |
||
45 | } |
||
46 | } |
||
47 |