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

nextras / orm / 43632413080

14 Nov 2026 07:30PM UTC coverage: 92.038% (-0.2%) from 92.222%
43632413080

Pull #852

github

web-flow
Merge f17a16229 into 07ee8e7ce
Pull Request #852: Add model rollback and transaction callbacks

153 of 177 new or added lines in 9 files covered. (86.44%)

4601 of 4999 relevant lines covered (92.04%)

6.26 hits per line

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

90.12
/src/Repository/IdentityMap.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * This file is part of the Nextras\Orm library.
5
 * This file was inspired by PetrP's ORM library https://github.com/PetrP/Orm/.
6
 * @link       https://github.com/nextras/orm
7
 */
8

9
namespace Nextras\Orm\Repository;
10

11

12
use DateTimeImmutable;
13
use Nextras\Orm\Entity\IEntity;
14
use Nextras\Orm\Entity\IEntityHasPreloadContainer;
15
use Nextras\Orm\Exception\InvalidArgumentException;
16
use Nextras\Orm\Exception\RollbackException;
17
use ReflectionClass;
18
use Throwable;
19

20

21
/**
22
 * @template E of IEntity
23
 */
24
class IdentityMap
25
{
26
        /** @var array<int|string, \WeakReference<E>|false> */
27
        private array $entities = [];
28

29
        /** @var array<int|string, bool> */
30
        private array $entitiesForRefresh = [];
31

32
        /** @var array<class-string<E>, ReflectionClass<E>> */
33
        private array $entityReflections = [];
34

35

36
        public function __construct(
5 ✔
37
                /** @var IRepository<E> */
38
                private readonly IRepository $repository,
2 ✔
39
        )
40
        {
41
        }
7 ✔
42

43

44
        /**
45
         * @param array|int|mixed $id
46
         */
47
        public function hasById($id): bool
48
        {
49
                $idHash = $this->getIdHash($id);
×
50
                return isset($this->entities[$idHash])
×
51
                        && ($this->entities[$idHash] === false || $this->entities[$idHash]->get() !== null);
×
52
        }
53

54

55
        /**
56
         * @param array|int|mixed $id
57
         * @return E|false|null
58
         */
59
        public function getById($id)
60
        {
61
                $idHash = $this->getIdHash($id);
7 ✔
62
                if (
63
                        !isset($this->entities[$idHash])
7 ✔
64
                        || $this->entities[$idHash] === false
7 ✔
65
                        || isset($this->entitiesForRefresh[$idHash])
7 ✔
66
                ) {
67
                        return null;
7 ✔
68
                }
69

70
                $entity = $this->entities[$idHash]->get();
7 ✔
71
                if ($entity instanceof IEntityHasPreloadContainer) {
7 ✔
72
                        $entity->setPreloadContainer(null);
7 ✔
73
                }
74
                return $entity;
7 ✔
75
        }
76

77

78
        /**
79
         * @param E $entity
80
         */
81
        public function add(IEntity $entity): void
82
        {
83
                $id = $this->getIdHash($entity->getPersistedId());
7 ✔
84
                $this->entities[$id] = \WeakReference::create($entity);
7 ✔
85
        }
7 ✔
86

87

88
        /**
89
         * @param array|int|mixed $id
90
         */
91
        public function remove($id): void
92
        {
93
                $idHash = $this->getIdHash($id);
7 ✔
94
                $this->entities[$idHash] = false;
7 ✔
95
                unset($this->entitiesForRefresh[$idHash]);
7 ✔
96
        }
7 ✔
97

98

99
        /**
100
         * @param array<string, mixed> $data
101
         * @return E|null
102
         */
103
        public function create(array $data): IEntity|null
104
        {
105
                $entity = $this->createEntity($data);
7 ✔
106
                $id = $this->getIdHash($entity->getPersistedId());
7 ✔
107

108
                if (isset($this->entities[$id])) {
7 ✔
109
                        if ($this->entities[$id] === false) {
7 ✔
110
                                $this->repository->detach($entity);
×
111
                                return null;
×
112
                        }
113
                        $existingEntity = $this->entities[$id]->get();
7 ✔
114
                        if ($existingEntity === null) {
7 ✔
115
                                // The entity was garbage collected, use the new one
116
                                $this->entities[$id] = \WeakReference::create($entity);
7 ✔
117
                                return $entity;
7 ✔
118
                        }
119
                        $this->repository->detach($entity);
7 ✔
120
                        if (isset($this->entitiesForRefresh[$id])) {
7 ✔
121
                                // entity can be detached because of delete try
122
                                $this->repository->attach($existingEntity);
7 ✔
123
                                $existingEntity->onRefresh($data);
7 ✔
124
                                unset($this->entitiesForRefresh[$id]);
7 ✔
125
                        }
126
                        return $existingEntity;
7 ✔
127
                }
128

129
                $this->entities[$id] = \WeakReference::create($entity);
7 ✔
130
                return $entity;
7 ✔
131
        }
132

133

134
        /**
135
         * @return list<E>
136
         */
137
        public function getAll(): array
138
        {
139
                $all = [];
7 ✔
140
                foreach ($this->entities as $entityRef) {
7 ✔
141
                        if ($entityRef !== false && ($entity = $entityRef->get()) !== null) {
7 ✔
142
                                $all[] = $entity;
7 ✔
143
                        }
144
                }
145
                return $all;
7 ✔
146
        }
147

148

149
        public function check(IEntity $entity): void
150
        {
151
                if (!in_array($entity::class, ($this->repository)::getEntityClassNames(), true)) {
7 ✔
152
                        throw new InvalidArgumentException("Entity '" . $entity::class . "' is not accepted by '" . $this->repository::class . "' repository.");
7 ✔
153
                }
154
        }
7 ✔
155

156

157
        public function destroyAllEntities(): void
158
        {
159
                $entities = $this->getAll();
7 ✔
160
                $this->entities = [];
7 ✔
161
                $this->entitiesForRefresh = [];
7 ✔
162
                $errors = [];
7 ✔
163
                foreach ($entities as $entity) {
7 ✔
164
                        try {
165
                                $this->repository->detach($entity);
7 ✔
166
                                $entity->onFree();
7 ✔
NEW
167
                        } catch (Throwable $e) {
×
NEW
168
                                $errors[] = $e;
×
169
                        }
170
                }
171
                if ($errors !== []) {
7 ✔
NEW
172
                        throw new RollbackException($errors);
×
173
                }
174
        }
7 ✔
175

176

177
        public function markForRefresh(IEntity $entity): void
178
        {
179
                $id = $this->getIdHash($entity->getPersistedId());
7 ✔
180
                $this->entitiesForRefresh[$id] = true;
7 ✔
181
        }
7 ✔
182

183

184
        public function isMarkedForRefresh(IEntity $entity): bool
185
        {
186
                $id = $this->getIdHash($entity->getPersistedId());
7 ✔
187
                return isset($this->entitiesForRefresh[$id]);
7 ✔
188
        }
189

190

191
        /**
192
         * @param array<string, mixed> $data
193
         * @return E
194
         */
195
        protected function createEntity(array $data): IEntity
196
        {
197
                $entityClass = $this->repository->getEntityClassName($data);
7 ✔
198
                $this->entityReflections[$entityClass] ??= new ReflectionClass($entityClass);
7 ✔
199
                $entity = $this->entityReflections[$entityClass]->newInstanceWithoutConstructor();
7 ✔
200
                $this->repository->attach($entity);
7 ✔
201
                $entity->onLoad($data);
7 ✔
202
                return $entity;
7 ✔
203
        }
204

205

206
        /**
207
         * @param mixed $id
208
         */
209
        protected function getIdHash($id): string
210
        {
211
                if (!is_array($id)) {
7 ✔
212
                        return $id instanceof DateTimeImmutable
7 ✔
213
                                ? $id->format('c.u')
7 ✔
214
                                : (string) $id;
7 ✔
215
                }
216

217
                return implode(
7 ✔
218
                        ',',
4 ✔
219
                        array_map(
7 ✔
220
                                fn($id): string => $id instanceof DateTimeImmutable
7 ✔
221
                                        ? $id->format('c.u')
7 ✔
222
                                        : (string) $id,
7 ✔
223
                                $id,
224
                        ),
225
                );
226
        }
227
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc