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

valkyrjaio / valkyrja / 16185661246

10 Jul 2025 03:48AM UTC coverage: 43.558% (-0.2%) from 43.747%
16185661246

push

github

MelechMizrachi
Http Routing: Updating ListCommand.

0 of 21 new or added lines in 1 file covered. (0.0%)

2913 existing lines in 212 files now uncovered.

3925 of 9011 relevant lines covered (43.56%)

11.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 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
    ) {
UNCOV
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
×
UNCOV
57
            ),
×
58
        );
×
59

UNCOV
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
    {
UNCOV
71
        $table  = $this->entity::getTableName();
×
72
        $select = $this->manager->createQueryBuilder()->select($table);
×
UNCOV
73
        $select->withWhere(...$where);
×
74

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

UNCOV
77
        return $this->mapResultsToEntity($statement->fetchAll())[0] ?? null;
×
78
    }
79

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

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

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

UNCOV
106
        return $this->mapResultsToEntity($statement->fetchAll());
×
107
    }
108

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

120
        // TODO: Implement create() method.
121
        // SET all values
122

UNCOV
123
        $this->manager->prepare((string) $create);
×
124

UNCOV
125
        $id = $this->manager->lastInsertId($table, $entity::getIdField());
×
126

UNCOV
127
        $entity->__set($entity::getIdField(), $id);
×
128
    }
129

130
    /**
131
     * @inheritDoc
132
     *
133
     * @param T $entity The entity
134
     */
135
    #[Override]
136
    public function update(Entity $entity): void
137
    {
138
        $table  = $entity::getTableName();
×
139
        $update = $this->manager->createQueryBuilder()->update($table);
×
140

141
        $where = new Where(
×
UNCOV
142
            value: new Value(
×
UNCOV
143
                name: $entity::getIdField(),
×
UNCOV
144
                value: $entity->getIdValue()
×
UNCOV
145
            ),
×
146
        );
×
147

UNCOV
148
        $update->withWhere($where);
×
149

150
        // TODO: Implement update() method.
151
        // SET all values
152

UNCOV
153
        $this->manager->prepare((string) $update);
×
154
    }
155

156
    /**
157
     * @inheritDoc
158
     *
159
     * @param T $entity The entity
160
     */
161
    #[Override]
162
    public function delete(Entity $entity): void
163
    {
164
        $table  = $entity::getTableName();
×
UNCOV
165
        $delete = $this->manager->createQueryBuilder()->delete($table);
×
166

UNCOV
167
        $where = new Where(
×
168
            value: new Value(
×
UNCOV
169
                name: $entity::getIdField(),
×
UNCOV
170
                value: $entity->getIdValue()
×
UNCOV
171
            ),
×
UNCOV
172
        );
×
173

UNCOV
174
        $delete->withWhere($where);
×
175

UNCOV
176
        $this->manager->prepare((string) $delete);
×
177
    }
178

179
    /**
180
     * @param array<string, mixed>[] $results The results
181
     *
182
     * @return T[]
183
     */
184
    protected function mapResultsToEntity(array $results): array
185
    {
186
        /** @var class-string<T> $entity */
UNCOV
187
        $entity = $this->entity;
×
188

UNCOV
189
        return array_map(
×
UNCOV
190
            static fn (array $data): Entity => $entity::fromArray($data),
×
UNCOV
191
            $results
×
UNCOV
192
        );
×
193
    }
194
}
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