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

valkyrjaio / valkyrja / 12764367500

14 Jan 2025 09:06AM UTC coverage: 47.411% (+0.07%) from 47.338%
12764367500

push

github

MelechMizrachi
Adding more specific array types.

24 of 68 new or added lines in 20 files covered. (35.29%)

13 existing lines in 8 files now uncovered.

5174 of 10913 relevant lines covered (47.41%)

19.07 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
 * @implements Contract<Entity>
37
 */
38
class Repository implements Contract
39
{
40
    /**
41
     * The persister.
42
     *
43
     * @var Persister
44
     */
45
    protected Persister $persister;
46

47
    /**
48
     * The retriever.
49
     *
50
     * @var Retriever
51
     */
52
    protected Retriever $retriever;
53

54
    /**
55
     * The relationships to get with each result.
56
     *
57
     * @var string[]|null
58
     */
59
    protected array|null $relationships = null;
60

61
    /**
62
     * Whether to get relations.
63
     *
64
     * @var bool
65
     */
66
    protected bool $getRelations = false;
67

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

84
        $this->persister = $this->driver->getPersister();
×
85
    }
86

87
    /**
88
     * @inheritDoc
89
     */
90
    public function find(): static
91
    {
92
        $this->retriever = $this->driver->createRetriever()->find($this->entity);
×
93

94
        return $this;
×
95
    }
96

97
    /**
98
     * @inheritDoc
99
     */
100
    public function findOne(int|string $id): static
101
    {
102
        $this->retriever = $this->driver->createRetriever()->findOne($this->entity, $id);
×
103

104
        return $this;
×
105
    }
106

107
    /**
108
     * @inheritDoc
109
     */
110
    public function count(): static
111
    {
112
        $this->retriever = $this->driver->createRetriever()->count($this->entity);
×
113

114
        return $this;
×
115
    }
116

117
    /**
118
     * @inheritDoc
119
     */
120
    public function columns(array $columns): static
121
    {
122
        $this->retriever->columns($columns);
×
123

124
        return $this;
×
125
    }
126

127
    /**
128
     * @inheritDoc
129
     */
130
    public function where(string $column, string|null $operator = null, mixed $value = null, bool $setType = true): static
131
    {
132
        $this->retriever->where($column, $operator, $value, $setType);
×
133

134
        return $this;
×
135
    }
136

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

144
        return $this;
×
145
    }
146

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

154
        return $this;
×
155
    }
156

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

164
        return $this;
×
165
    }
166

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

180
        return $this;
×
181
    }
182

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

190
        return $this;
×
191
    }
192

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

200
        return $this;
×
201
    }
202

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

210
        return $this;
×
211
    }
212

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

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

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

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

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

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

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

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

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

278
        $this->persister->delete($entity, $defer);
×
279
    }
280

281
    /**
282
     * @inheritDoc
283
     *
284
     * @throws InvalidEntityException
285
     */
286
    public function softDelete(SoftDeleteEntity $entity, bool $defer = true): void
287
    {
288
        $this->validateEntity($entity);
×
289

290
        $this->persister->softDelete($entity, $defer);
×
291
    }
292

293
    /**
294
     * @inheritDoc
295
     *
296
     * @throws InvalidEntityException
297
     */
298
    public function clear(Entity|null $entity = null): void
299
    {
300
        if ($entity !== null) {
×
301
            $this->validateEntity($entity);
×
302
        }
303

304
        $this->persister->clear($entity);
×
305
    }
306

307
    /**
308
     * @inheritDoc
309
     */
310
    public function persist(): bool
311
    {
312
        return $this->persister->persist();
×
313
    }
314

315
    /**
316
     * @inheritDoc
317
     */
318
    public function createQueryBuilder(string|null $alias = null): QueryBuilder
319
    {
320
        return $this->driver->createQueryBuilder($this->entity, $alias);
×
321
    }
322

323
    /**
324
     * @inheritDoc
325
     */
326
    public function createQuery(string $query): Query
327
    {
328
        return $this->driver->createQuery($query, $this->entity);
×
329
    }
330

331
    /**
332
     * @inheritDoc
333
     */
334
    public function getRetriever(): Retriever
335
    {
336
        return $this->retriever;
×
337
    }
338

339
    /**
340
     * @inheritDoc
341
     */
342
    public function getPersister(): Persister
343
    {
344
        return $this->persister;
×
345
    }
346

347
    /**
348
     * Validate the passed entity.
349
     *
350
     * @param Entity $entity The entity
351
     *
352
     * @throws InvalidEntityException
353
     *
354
     * @return void
355
     */
356
    protected function validateEntity(Entity $entity): void
357
    {
358
        if (! ($entity instanceof $this->entity)) {
×
359
            throw new InvalidEntityException(
×
360
                'This repository expects entities to be instances of '
×
361
                . $this->entity
×
362
                . '. Entity instanced from '
×
363
                . $entity::class
×
364
                . ' provided instead.'
×
365
            );
×
366
        }
367
    }
368
}
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