• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wimski / laravel-model-repositories / 13542544090

26 Feb 2025 11:11AM UTC coverage: 98.765% (-1.2%) from 100.0%
13542544090

push

github

wimski
normalize composer.json

160 of 162 relevant lines covered (98.77%)

13.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.55
/src/Repositories/AbstractModelRepository.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Wimski\ModelRepositories\Repositories;
6

7
use Closure;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Database\Eloquent\Collection;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Database\Eloquent\ModelNotFoundException;
13
use Illuminate\Database\Query\Expression;
14
use Illuminate\Support\LazyCollection;
15
use Wimski\ModelRepositories\Contracts\Repositories\ModelRepositoryInterface;
16

17
/**
18
 * @template TModel of Model
19
 * @implements ModelRepositoryInterface<TModel>
20
 */
21
abstract readonly class AbstractModelRepository implements ModelRepositoryInterface
22
{
23
    /**
24
     * @param TModel $model
25
     */
26
    public function __construct(
27
        protected Model $model,
28
    ) {
29
    }
42✔
30

31
    public function builder(bool $withGlobalScopes = true): Builder
32
    {
33
        $builder = $this->model->newQueryWithoutScopes();
42✔
34

35
        if ($withGlobalScopes) {
42✔
36
            $this->model->registerGlobalScopes($builder);
41✔
37
        }
38

39
        return $builder;
42✔
40
    }
41

42
    public function find(int|string $key, string ...$column): ?Model
43
    {
44
        return $this->builder()->find($key, $this->parseColumns(...$column));
4✔
45
    }
46

47
    public function findOrFail(int|string $key, string ...$column): Model
48
    {
49
        return $this->builder()->findOrFail($key, $this->parseColumns(...$column));
4✔
50
    }
51

52
    public function findMany(Arrayable|array $keys, string ...$column): Collection
53
    {
54
        /** @var Arrayable<array-key, mixed>|array<array-key, int|string> $keys */
55
        return $this->builder()->findMany($keys, $this->parseColumns(...$column));
3✔
56
    }
57

58
    public function first(string ...$column): ?Model
59
    {
60
        return $this->builder()->first($this->parseColumns(...$column));
4✔
61
    }
62

63
    public function firstOrFail(string ...$column): Model
64
    {
65
        return $this->builder()->firstOrFail($this->parseColumns(...$column));
4✔
66
    }
67

68
    public function firstWhere(string|array|Closure|Expression $column, mixed $operator = null, mixed $value = null, string $boolean = 'and'): ?Model
69
    {
70
        return $this->builder()->firstWhere($column, $operator, $value, $boolean);
4✔
71
    }
72

73
    public function firstWhereOrFail(string|array|Closure|Expression$column, mixed $operator = null, mixed $value = null, string $boolean = 'and'): Model
74
    {
75
        $model = $this->firstWhere($column, $operator, $value, $boolean);
2✔
76

77
        if ($model === null) {
2✔
78
            throw $this->makeModelNotFoundException();
1✔
79
        }
80

81
        return $model;
1✔
82
    }
83

84
    public function where(string|array|Closure|Expression$column, mixed $operator = null, mixed $value = null, string $boolean = 'and'): Collection
85
    {
86
        return $this->builder()->where($column, $operator, $value, $boolean)->get();
1✔
87
    }
88

89
    public function whereIn(string $column, array $values): Collection
90
    {
91
        return $this->builder()->whereIn($column, $values)->get();
1✔
92
    }
93

94
    public function whereNotIn(string $column, array $values): Collection
95
    {
96
        return $this->builder()->whereNotIn($column, $values)->get();
1✔
97
    }
98

99
    public function cursor(): LazyCollection
100
    {
101
        return $this->builder()->cursor();
1✔
102
    }
103

104
    public function all(string ...$column): Collection
105
    {
106
        return $this->builder()->get($this->parseColumns(...$column));
3✔
107
    }
108

109
    public function make(array $attributes): Model
110
    {
111
        return $this->builder()->make($attributes);
1✔
112
    }
113

114
    public function findOrMake(int|string $key, string ...$column): Model
115
    {
116
        return $this->builder()->findOrNew($key, $this->parseColumns(...$column));
4✔
117
    }
118

119
    public function firstWhereOrMake(array $attributes, array $values = []): Model
120
    {
121
        return $this->builder()->firstOrNew($attributes, $values);
2✔
122
    }
123

124
    public function create(array $attributes): Model
125
    {
126
        return $this->builder()->create($attributes);
1✔
127
    }
128

129
    public function firstWhereOrCreate(array $attributes, array $values = []): Model
130
    {
131
        return $this->builder()->firstOrCreate($attributes, $values);
2✔
132
    }
133

134
    /**
135
     * @return array<int, string>
136
     */
137
    protected function parseColumns(string ...$column): array
138
    {
139
        return empty($column) ? ['*'] : array_values($column);
26✔
140
    }
141

142
    /**
143
     * @throws ModelNotFoundException
144
     */
145
    protected function throwModelNotFoundException(int|string ...$key): void
146
    {
147
        throw $this->makeModelNotFoundException(...$key);
×
148
    }
149

150
    /**
151
     * @return ModelNotFoundException<TModel>
152
     */
153
    protected function makeModelNotFoundException(int|string ...$key): ModelNotFoundException
154
    {
155
        /** @var ModelNotFoundException<TModel> $exception */
156
        $exception = (new ModelNotFoundException())->setModel(get_class($this->model), array_values($key));
1✔
157

158
        return $exception;
1✔
159
    }
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc