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

valkyrjaio / valkyrja / 16186589328

10 Jul 2025 05:04AM UTC coverage: 43.44% (-0.1%) from 43.558%
16186589328

push

github

MelechMizrachi
Orm: Completing Repository, and updating Statement to take an entity class to create an array of entities instead of associative array return.

4 of 47 new or added lines in 3 files covered. (8.51%)

2 existing lines in 2 files now uncovered.

3927 of 9040 relevant lines covered (43.44%)

11.04 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 Override;
17
use Valkyrja\Orm\Contract\Manager;
18
use Valkyrja\Orm\Data\Value;
19
use Valkyrja\Orm\Data\Where;
20
use Valkyrja\Orm\Entity\Contract\Entity;
21
use Valkyrja\Orm\Repository\Contract\Repository as Contract;
22

23
/**
24
 * Class Repository.
25
 *
26
 * @author Melech Mizrachi
27
 *
28
 * @template T of Entity
29
 *
30
 * @implements Contract<T>
31
 */
32
class Repository implements Contract
33
{
34
    /**
35
     * @param class-string<T> $entity
36
     */
37
    public function __construct(
38
        protected Manager $manager,
39
        protected string $entity,
40
    ) {
41
    }
×
42

43
    /**
44
     * @inheritDoc
45
     *
46
     * @return T|null
47
     */
48
    #[Override]
49
    public function find(int|string $id): Entity|null
50
    {
51
        /** @var class-string<T> $entity */
52
        $entity = $this->entity;
×
53
        $where  = new Where(
×
54
            value: new Value(
×
55
                name: $entity::getIdField(),
×
56
                value: $id
×
57
            ),
×
58
        );
×
59

60
        return $this->findBy($where);
×
61
    }
62

63
    /**
64
     * @inheritDoc
65
     *
66
     * @return T|null
67
     */
68
    #[Override]
69
    public function findBy(Where ...$where): Entity|null
70
    {
71
        $table  = $this->entity::getTableName();
×
72
        $select = $this->manager->createQueryBuilder()->select($table);
×
73
        $select->withWhere(...$where);
×
74

75
        $statement = $this->manager->prepare((string) $select);
×
76

NEW
77
        $fetch = $statement->fetchAll($this->entity);
×
78

NEW
79
        return $fetch[0] ?? null;
×
80
    }
81

82
    /**
83
     * @inheritDoc
84
     *
85
     * @return T[]
86
     */
87
    #[Override]
88
    public function all(): array
89
    {
90
        return $this->allBy();
×
91
    }
92

93
    /**
94
     * @inheritDoc
95
     *
96
     * @return T[]
97
     */
98
    #[Override]
99
    public function allBy(Where ...$where): array
100
    {
101
        $table  = $this->entity::getTableName();
×
102
        $select = $this->manager->createQueryBuilder()->select($table);
×
103
        $select->withWhere(...$where);
×
104

UNCOV
105
        $statement = $this->manager->prepare((string) $select);
×
106

NEW
107
        return $statement->fetchAll($this->entity);
×
108
    }
109

110
    /**
111
     * @inheritDoc
112
     *
113
     * @param T $entity The entity
114
     */
115
    #[Override]
116
    public function create(Entity $entity): void
117
    {
NEW
118
        $table = $entity::getTableName();
×
119

NEW
120
        $set = [];
×
121

NEW
122
        foreach ($entity->asStorableArray() as $key => $value) {
×
NEW
123
            $set[] = new Value(
×
NEW
124
                name: $key,
×
NEW
125
                value: $value
×
NEW
126
            );
×
127
        }
128

NEW
129
        $create = $this->manager
×
NEW
130
            ->createQueryBuilder()
×
NEW
131
            ->insert($table)
×
NEW
132
            ->withSet(...$set);
×
133

134
        $this->manager->prepare((string) $create);
×
135

136
        $id = $this->manager->lastInsertId($table, $entity::getIdField());
×
137

138
        $entity->__set($entity::getIdField(), $id);
×
139
    }
140

141
    /**
142
     * @inheritDoc
143
     *
144
     * @param T $entity The entity
145
     */
146
    #[Override]
147
    public function update(Entity $entity): void
148
    {
NEW
149
        $table = $entity::getTableName();
×
150

151
        $where = new Where(
×
152
            value: new Value(
×
153
                name: $entity::getIdField(),
×
154
                value: $entity->getIdValue()
×
155
            ),
×
156
        );
×
157

NEW
158
        $set = [];
×
159

NEW
160
        foreach ($entity->asStorableChangedArray() as $key => $value) {
×
NEW
161
            $set[] = new Value(
×
NEW
162
                name: $key,
×
NEW
163
                value: $value
×
NEW
164
            );
×
165
        }
166

NEW
167
        $update = $this->manager
×
NEW
168
            ->createQueryBuilder()
×
NEW
169
            ->update($table)
×
NEW
170
            ->withWhere($where)
×
NEW
171
            ->withSet(...$set);
×
172

173
        $this->manager->prepare((string) $update);
×
174
    }
175

176
    /**
177
     * @inheritDoc
178
     *
179
     * @param T $entity The entity
180
     */
181
    #[Override]
182
    public function delete(Entity $entity): void
183
    {
NEW
184
        $table = $entity::getTableName();
×
185

186
        $where = new Where(
×
187
            value: new Value(
×
188
                name: $entity::getIdField(),
×
189
                value: $entity->getIdValue()
×
190
            ),
×
191
        );
×
192

NEW
193
        $delete = $this->manager
×
NEW
194
            ->createQueryBuilder()
×
NEW
195
            ->delete($table)
×
NEW
196
            ->withWhere($where);
×
197

198
        $this->manager->prepare((string) $delete);
×
199
    }
200
}
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