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

nextras / orm / 29191903500

12 Jul 2026 12:02PM UTC coverage: 91.963% (-0.2%) from 92.123%
29191903500

Pull #820

github

web-flow
Merge 8dab47905 into fc483811c
Pull Request #820: refactor OrmExtension for NetteDI (BC break!)

135 of 158 new or added lines in 8 files covered. (85.44%)

1 existing line in 1 file now uncovered.

4348 of 4728 relevant lines covered (91.96%)

5.39 hits per line

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

92.59
/src/Model/Model.php
1
<?php declare(strict_types = 1);
2

3
namespace Nextras\Orm\Model;
4

5

6
use Nette\SmartObject;
7
use Nextras\Orm\Entity\IEntity;
8
use Nextras\Orm\Exception\InvalidArgumentException;
9
use Nextras\Orm\Relationships\IRelationshipCollection;
10
use Nextras\Orm\Relationships\IRelationshipContainer;
11
use Nextras\Orm\Repository\IRepository;
12
use Nextras\Orm\Repository\PersistenceHelper;
13
use Nextras\Orm\Repository\PersistenceMode;
14
use function array_merge;
15
use function get_class;
16
use function is_string;
17

18

19
class Model implements IModel
20
{
21
        use SmartObject;
22

23
        /** @var list<callable(IEntity[] $persisted, IEntity[] $removed): void> */
24
        public array $onFlush = [];
25

26

27
        public function __construct(
5✔
28
                private readonly IRepositoryLoader $repositoryLoader,
1✔
29
                private readonly MetadataStorage $metadataStorage
1✔
30
        )
31
        {
32
        }
6✔
33

34

35
        public function hasRepositoryByName(string $name): bool
36
        {
NEW
37
                return $this->repositoryLoader->hasRepositoryByName($name);
×
38
        }
39

40

41
        public function getRepositoryByName(string $name): IRepository
42
        {
43
                return $this->repositoryLoader->getRepositoryByName($name)
6✔
44
                        ?? throw new InvalidArgumentException("Repository with '$name' simple name does not exist.");
6✔
45
        }
46

47

48
        public function hasRepository(string $className): bool
49
        {
NEW
50
                return $this->repositoryLoader->hasRepository($className);
×
51
        }
52

53

54
        public function getRepository(string $className): IRepository
55
        {
56
                return $this->repositoryLoader->getRepository($className)
6✔
57
                        ?? throw new InvalidArgumentException("Repository with '$className' class name does not exist.");
6✔
58
        }
59

60

61
        public function getRepositoryForEntity($entity): IRepository
62
        {
63
                $entityClassName = is_string($entity) ? $entity : get_class($entity);
6✔
64
                $repositoryClassName = $this->repositoryLoader->getRepositoryClassNameForEntity($entityClassName)
6✔
NEW
65
                        ?? throw new InvalidArgumentException("No repository manages '$entityClassName' entity.");
×
66
                return $this->getRepository($repositoryClassName);
6✔
67
        }
68

69

70
        public function getMetadataStorage(): MetadataStorage
71
        {
72
                return $this->metadataStorage;
×
73
        }
74

75

76
        public function persist(IEntity $entity, bool $withCascade = true): IEntity
77
        {
78
                $this->processPersist(mode: PersistenceMode::Persist, entity: $entity, withCascade: $withCascade);
6✔
79
                return $entity;
6✔
80
        }
81

82

83
        public function persistAndFlush(IEntity $entity): IEntity
84
        {
85
                $this->persist($entity);
6✔
86
                $this->flush();
6✔
87
                return $entity;
6✔
88
        }
89

90

91
        public function remove(IEntity $entity, bool $withCascade = true): IEntity
92
        {
93
                $this->processPersist(mode: PersistenceMode::Remove, entity: $entity, withCascade: $withCascade);
6✔
94
                return $entity;
6✔
95
        }
96

97

98
        public function removeAndFlush(IEntity $entity, bool $withCascade = true): IEntity
99
        {
100
                $this->remove($entity, $withCascade);
6✔
101
                $this->flush();
6✔
102
                return $entity;
6✔
103
        }
104

105

106
        public function flush(): void
107
        {
108
                $allPersisted = [];
6✔
109
                $allRemoved = [];
6✔
110
                foreach ($this->repositoryLoader->getInitializedRepositories() as $repository) {
6✔
111
                        [$persisted, $removed] = $repository->doFlush();
6✔
112
                        $allPersisted = array_merge($allPersisted, $persisted);
6✔
113
                        $allRemoved = array_merge($allRemoved, $removed);
6✔
114
                }
115

116
                $this->onFlush($allPersisted, $allRemoved);
6✔
117
        }
6✔
118

119

120
        public function clear(): void
121
        {
122
                foreach ($this->repositoryLoader->getInitializedRepositories() as $repository) {
6✔
123
                        $repository->doClear();
6✔
124
                }
125
        }
6✔
126

127

128
        public function refreshAll(bool $allowOverwrite = false): void
129
        {
130
                foreach ($this->repositoryLoader->getInitializedRepositories() as $repository) {
6✔
131
                        $repository->doRefreshAll($allowOverwrite);
6✔
132
                }
133
        }
6✔
134

135

136
        /**
137
         * Returns repository by its name.
138
         * @return IRepository<*>
139
         */
140
        public function &__get(string $name): IRepository
141
        {
142
                $repository = $this->getRepositoryByName($name);
6✔
143
                return $repository;
6✔
144
        }
145

146

147
        protected function processPersist(PersistenceMode $mode, IEntity $entity, bool $withCascade): void
148
        {
149
                [$queuePersist, $queueRemove] = PersistenceHelper::getCascadeQueue($entity, $mode, $this, $withCascade);
6✔
150
                foreach ($queuePersist as $object) {
6✔
151
                        if ($object instanceof IEntity) {
6✔
152
                                $repository = $this->getRepositoryForEntity($object);
6✔
153
                                $repository->doPersist($object);
6✔
154
                        } elseif ($object instanceof IRelationshipCollection) {
6✔
155
                                $object->doPersist();
6✔
156
                        } elseif ($object instanceof IRelationshipContainer) {
6✔
157
                                $object->doPersist();
6✔
158
                        }
159
                }
160
                foreach ($queueRemove as $object) {
6✔
161
                        $repository = $this->getRepositoryForEntity($object);
6✔
162
                        $repository->doRemove($object);
6✔
163
                }
164
        }
6✔
165
}
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