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

valkyrjaio / valkyrja / 15659546660

15 Jun 2025 04:39AM UTC coverage: 47.202% (-0.4%) from 47.589%
15659546660

push

github

MelechMizrachi
Update Config.

5331 of 11294 relevant lines covered (47.2%)

16.11 hits per line

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

0.0
/src/Valkyrja/Orm/Repository/Repository.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <melechmizrachi@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Valkyrja\Orm\Repository;
15

16
use InvalidArgumentException;
17
use Valkyrja\Orm\Contract\Orm;
18
use Valkyrja\Orm\Driver\Contract\Driver;
19
use Valkyrja\Orm\Entity\Contract\Entity;
20
use Valkyrja\Orm\Entity\Contract\SoftDeleteEntity;
21
use Valkyrja\Orm\Enum\WhereType;
22
use Valkyrja\Orm\Exception\InvalidEntityException;
23
use Valkyrja\Orm\Persister\Contract\Persister;
24
use Valkyrja\Orm\Query\Contract\Query;
25
use Valkyrja\Orm\QueryBuilder\Contract\QueryBuilder;
26
use Valkyrja\Orm\Repository\Contract\Repository as Contract;
27
use Valkyrja\Orm\Retriever\Contract\Retriever;
28

29
use function assert;
30

31
/**
32
 * Class Repository.
33
 *
34
 * @author Melech Mizrachi
35
 *
36
 * @template Entity of Entity
37
 *
38
 * @implements Contract<Entity>
39
 */
40
class Repository implements Contract
41
{
42
    /**
43
     * The retriever.
44
     *
45
     * @var Retriever<Entity>
46
     */
47
    protected Retriever $retriever;
48

49
    /**
50
     * The relationships to get with each result.
51
     *
52
     * @var string[]|null
53
     */
54
    protected array|null $relationships = null;
55

56
    /**
57
     * Whether to get relations.
58
     *
59
     * @var bool
60
     */
61
    protected bool $getRelations = false;
62

63
    /**
64
     * Repository constructor.
65
     *
66
     * @param Orm                  $orm       The orm manager
67
     * @param Driver               $driver    The driver
68
     * @param Persister<Entity>    $persister The persister
69
     * @param class-string<Entity> $entity    The entity class name
70
     *
71
     * @throws InvalidArgumentException
72
     */
73
    public function __construct(
74
        protected Orm $orm,
75
        protected Driver $driver,
76
        protected Persister $persister,
77
        protected string $entity
78
    ) {
79
        assert(is_a($entity, Entity::class, true));
×
80
    }
81

82
    /**
83
     * @inheritDoc
84
     */
85
    public function find(): static
86
    {
87
        $this->retriever = $this->driver->createRetriever()->find($this->entity);
×
88

89
        return $this;
×
90
    }
91

92
    /**
93
     * @inheritDoc
94
     */
95
    public function findOne(int|string $id): static
96
    {
97
        $this->retriever = $this->driver->createRetriever()->findOne($this->entity, $id);
×
98

99
        return $this;
×
100
    }
101

102
    /**
103
     * @inheritDoc
104
     */
105
    public function count(): static
106
    {
107
        $this->retriever = $this->driver->createRetriever()->count($this->entity);
×
108

109
        return $this;
×
110
    }
111

112
    /**
113
     * @inheritDoc
114
     */
115
    public function columns(string ...$columns): static
116
    {
117
        $this->retriever->columns(...$columns);
×
118

119
        return $this;
×
120
    }
121

122
    /**
123
     * @inheritDoc
124
     */
125
    public function where(
126
        string $column,
127
        string|null $operator = null,
128
        QueryBuilder|array|string|float|int|bool|null $value = null,
129
        bool $setType = true
130
    ): static {
131
        $this->retriever->where($column, $operator, $value, $setType);
×
132

133
        return $this;
×
134
    }
135

136
    /**
137
     * @inheritDoc
138
     */
139
    public function startWhereGroup(): static
140
    {
141
        $this->retriever->startWhereGroup();
×
142

143
        return $this;
×
144
    }
145

146
    /**
147
     * @inheritDoc
148
     */
149
    public function endWhereGroup(): static
150
    {
151
        $this->retriever->endWhereGroup();
×
152

153
        return $this;
×
154
    }
155

156
    /**
157
     * @inheritDoc
158
     */
159
    public function whereType(WhereType $type = WhereType::AND): static
160
    {
161
        $this->retriever->whereType($type);
×
162

163
        return $this;
×
164
    }
165

166
    /**
167
     * @inheritDoc
168
     */
169
    public function join(
170
        string $table,
171
        string $column1,
172
        string $column2,
173
        string|null $operator = null,
174
        string|null $type = null,
175
        bool|null $isWhere = null
176
    ): static {
177
        $this->retriever->join($table, $column1, $column2, $operator, $type, $isWhere);
×
178

179
        return $this;
×
180
    }
181

182
    /**
183
     * @inheritDoc
184
     */
185
    public function orderBy(string $column, string|null $direction = null): static
186
    {
187
        $this->retriever->orderBy($column, $direction);
×
188

189
        return $this;
×
190
    }
191

192
    /**
193
     * @inheritDoc
194
     */
195
    public function limit(int $limit): static
196
    {
197
        $this->retriever->limit($limit);
×
198

199
        return $this;
×
200
    }
201

202
    /**
203
     * @inheritDoc
204
     */
205
    public function offset(int $offset): static
206
    {
207
        $this->retriever->offset($offset);
×
208

209
        return $this;
×
210
    }
211

212
    /**
213
     * @inheritDoc
214
     */
215
    public function getResult(): array
216
    {
217
        return $this->retriever->getResult();
×
218
    }
219

220
    /**
221
     * @inheritDoc
222
     */
223
    public function getOneOrNull(): Entity|null
224
    {
225
        return $this->getResult()[0] ?? null;
×
226
    }
227

228
    /**
229
     * @inheritDoc
230
     */
231
    public function getOneOrFail(): Entity
232
    {
233
        return $this->retriever->getOneOrFail();
×
234
    }
235

236
    /**
237
     * @inheritDoc
238
     */
239
    public function getCount(): int
240
    {
241
        return $this->retriever->getCount();
×
242
    }
243

244
    /**
245
     * @inheritDoc
246
     *
247
     * @throws InvalidEntityException
248
     */
249
    public function create(Entity $entity, bool $defer = true): void
250
    {
251
        $this->validateEntity($entity);
×
252

253
        $this->persister->create($entity, $defer);
×
254
    }
255

256
    /**
257
     * @inheritDoc
258
     *
259
     * @throws InvalidEntityException
260
     */
261
    public function save(Entity $entity, bool $defer = true): void
262
    {
263
        $this->validateEntity($entity);
×
264

265
        $this->persister->save($entity, $defer);
×
266
    }
267

268
    /**
269
     * @inheritDoc
270
     *
271
     * @throws InvalidEntityException
272
     */
273
    public function delete(Entity $entity, bool $defer = true): void
274
    {
275
        $this->validateEntity($entity);
×
276

277
        if ($entity instanceof SoftDeleteEntity) {
×
278
            $this->persister->save($entity, $defer);
×
279

280
            return;
×
281
        }
282

283
        $this->persister->delete($entity, $defer);
×
284
    }
285

286
    /**
287
     * @inheritDoc
288
     *
289
     * @throws InvalidEntityException
290
     */
291
    public function clear(Entity|null $entity = null): void
292
    {
293
        if ($entity !== null) {
×
294
            $this->validateEntity($entity);
×
295
        }
296

297
        $this->persister->clear($entity);
×
298
    }
299

300
    /**
301
     * @inheritDoc
302
     */
303
    public function persist(): bool
304
    {
305
        return $this->persister->persist();
×
306
    }
307

308
    /**
309
     * @inheritDoc
310
     */
311
    public function createQueryBuilder(string|null $alias = null): QueryBuilder
312
    {
313
        return $this->driver->createQueryBuilder($this->entity, $alias);
×
314
    }
315

316
    /**
317
     * @inheritDoc
318
     */
319
    public function createQuery(string $query): Query
320
    {
321
        return $this->driver->createQuery($query, $this->entity);
×
322
    }
323

324
    /**
325
     * @inheritDoc
326
     */
327
    public function getRetriever(): Retriever
328
    {
329
        return $this->retriever;
×
330
    }
331

332
    /**
333
     * @inheritDoc
334
     */
335
    public function getPersister(): Persister
336
    {
337
        return $this->persister;
×
338
    }
339

340
    /**
341
     * Validate the passed entity.
342
     *
343
     * @throws InvalidEntityException
344
     */
345
    protected function validateEntity(Entity $entity): void
346
    {
347
        if (! ($entity instanceof $this->entity)) {
×
348
            throw new InvalidEntityException(
×
349
                'This repository expects entities to be instances of '
×
350
                . $this->entity
×
351
                . '. Entity instanced from '
×
352
                . $entity::class
×
353
                . ' provided instead.'
×
354
            );
×
355
        }
356
    }
357
}
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