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

valkyrjaio / valkyrja / 13047041070

30 Jan 2025 06:43AM UTC coverage: 47.621% (+0.2%) from 47.422%
13047041070

push

github

MelechMizrachi
PHPStan level 7 and 8.

168 of 1038 new or added lines in 111 files covered. (16.18%)

444 existing lines in 45 files now uncovered.

5195 of 10909 relevant lines covered (47.62%)

18.83 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(array $columns): static
116
    {
117
        $this->retriever->columns($columns);
×
118

119
        return $this;
×
120
    }
121

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

129
        return $this;
×
130
    }
131

132
    /**
133
     * @inheritDoc
134
     */
135
    public function startWhereGroup(): static
136
    {
137
        $this->retriever->startWhereGroup();
×
138

139
        return $this;
×
140
    }
141

142
    /**
143
     * @inheritDoc
144
     */
145
    public function endWhereGroup(): static
146
    {
147
        $this->retriever->endWhereGroup();
×
148

149
        return $this;
×
150
    }
151

152
    /**
153
     * @inheritDoc
154
     */
155
    public function whereType(WhereType $type = WhereType::AND): static
156
    {
157
        $this->retriever->whereType($type);
×
158

159
        return $this;
×
160
    }
161

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

175
        return $this;
×
176
    }
177

178
    /**
179
     * @inheritDoc
180
     */
181
    public function orderBy(string $column, string|null $direction = null): static
182
    {
183
        $this->retriever->orderBy($column, $direction);
×
184

185
        return $this;
×
186
    }
187

188
    /**
189
     * @inheritDoc
190
     */
191
    public function limit(int $limit): static
192
    {
193
        $this->retriever->limit($limit);
×
194

195
        return $this;
×
196
    }
197

198
    /**
199
     * @inheritDoc
200
     */
201
    public function offset(int $offset): static
202
    {
203
        $this->retriever->offset($offset);
×
204

205
        return $this;
×
206
    }
207

208
    /**
209
     * @inheritDoc
210
     */
211
    public function getResult(): array
212
    {
213
        return $this->retriever->getResult();
×
214
    }
215

216
    /**
217
     * @inheritDoc
218
     */
219
    public function getOneOrNull(): Entity|null
220
    {
221
        return $this->getResult()[0] ?? null;
×
222
    }
223

224
    /**
225
     * @inheritDoc
226
     */
227
    public function getOneOrFail(): Entity
228
    {
229
        return $this->retriever->getOneOrFail();
×
230
    }
231

232
    /**
233
     * @inheritDoc
234
     */
235
    public function getCount(): int
236
    {
237
        return $this->retriever->getCount();
×
238
    }
239

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

249
        $this->persister->create($entity, $defer);
×
250
    }
251

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

261
        $this->persister->save($entity, $defer);
×
262
    }
263

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

NEW
273
        if ($entity instanceof SoftDeleteEntity) {
×
NEW
274
            $this->persister->save($entity, $defer);
×
275

NEW
276
            return;
×
277
        }
278

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

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

293
        $this->persister->clear($entity);
×
294
    }
295

296
    /**
297
     * @inheritDoc
298
     */
299
    public function persist(): bool
300
    {
301
        return $this->persister->persist();
×
302
    }
303

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

312
    /**
313
     * @inheritDoc
314
     */
315
    public function createQuery(string $query): Query
316
    {
317
        return $this->driver->createQuery($query, $this->entity);
×
318
    }
319

320
    /**
321
     * @inheritDoc
322
     */
323
    public function getRetriever(): Retriever
324
    {
325
        return $this->retriever;
×
326
    }
327

328
    /**
329
     * @inheritDoc
330
     */
331
    public function getPersister(): Persister
332
    {
333
        return $this->persister;
×
334
    }
335

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