1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Tfboe\FmLib\Service; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\AbstractLazyCollection; |
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use Doctrine\Common\Collections\Collection; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
11
|
|
|
use Tfboe\FmLib\Entity\CompetitionInterface; |
12
|
|
|
use Tfboe\FmLib\Entity\GameInterface; |
13
|
|
|
use Tfboe\FmLib\Entity\Helpers\IdAble; |
14
|
|
|
use Tfboe\FmLib\Entity\MatchInterface; |
15
|
|
|
use Tfboe\FmLib\Entity\PhaseInterface; |
16
|
|
|
use Tfboe\FmLib\Entity\RankingInterface; |
17
|
|
|
use Tfboe\FmLib\Entity\TeamInterface; |
18
|
|
|
use Tfboe\FmLib\Entity\TeamMembershipInterface; |
19
|
|
|
use Tfboe\FmLib\Entity\TournamentInterface; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class LoadingService |
24
|
|
|
* @package App\Service |
25
|
|
|
*/ |
26
|
|
|
class LoadingService implements LoadingServiceInterface |
27
|
|
|
{ |
28
|
|
|
//<editor-fold desc="Fields"> |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $defaultPropertiesToLoad; |
33
|
|
|
/** |
34
|
|
|
* @var EntityManager |
35
|
|
|
*/ |
36
|
|
|
private $em; |
37
|
|
|
//</editor-fold desc="Fields"> |
38
|
|
|
|
39
|
|
|
//<editor-fold desc="Constructor"> |
40
|
|
|
/** |
41
|
|
|
* LoadingService constructor. |
42
|
|
|
* @param EntityManagerInterface $em |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
EntityManagerInterface $em |
46
|
|
|
) |
47
|
|
|
{ |
48
|
|
|
$this->em = $em; |
|
|
|
|
49
|
|
|
$this->defaultPropertiesToLoad = [ |
|
|
|
|
50
|
|
|
TournamentInterface::class => [["competitions"]], |
51
|
|
|
CompetitionInterface::class => [["teams"], ["phases"]], |
52
|
|
|
TeamInterface::class => [["memberships"]], |
53
|
|
|
TeamMembershipInterface::class => [["player", "team"]], |
54
|
|
|
PhaseInterface::class => [["preQualifications"], ["postQualifications"], ["rankings"], ["matches"]], |
55
|
|
|
RankingInterface::class => [["teams"]], |
56
|
|
|
MatchInterface::class => [["rankingsA", "rankingsB"], ["games"]], |
57
|
|
|
GameInterface::class => [["playersA", "playersB"]], |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
//</editor-fold desc="Constructor"> |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
//<editor-fold desc="Public Methods"> |
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
|
|
public function loadEntities(array $entities, ?array $propertyMap = null) |
68
|
|
|
{ |
69
|
|
|
if ($propertyMap === null) { |
70
|
|
|
$propertyMap = $this->defaultPropertiesToLoad; |
71
|
|
|
} |
72
|
|
|
//build groups for each type |
73
|
|
|
$toDoEntityIds = []; |
74
|
|
|
$done = []; |
75
|
|
|
foreach ($entities as $entity) { |
76
|
|
|
if (!array_key_exists($entity->getEntityId(), $done)) { |
77
|
|
|
$done[$entity->getEntityId()] = true; |
78
|
|
|
$class = $this->keyOfPropertyMap($entity, $propertyMap); |
|
|
|
|
79
|
|
|
if ($class !== null) { |
80
|
|
|
if (!array_key_exists($class, $toDoEntityIds)) { |
81
|
|
|
$toDoEntityIds[$class] = []; |
82
|
|
|
} |
83
|
|
|
$toDoEntityIds[$class][] = $entity; |
84
|
|
|
break; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
while (count($toDoEntityIds) > 0) { |
89
|
|
|
$entities = reset($toDoEntityIds); |
90
|
|
|
|
91
|
|
|
$class = key($toDoEntityIds); |
92
|
|
|
unset($toDoEntityIds[$class]); |
93
|
|
|
foreach ($propertyMap[$class] as $properties) { |
94
|
|
|
//eliminate entities which have already loaded the properties |
95
|
|
|
$ids = array_map(function (IdAble $e) { |
96
|
|
|
return $e->getEntityId(); |
97
|
|
|
}, array_filter($entities, function (IdAble $e) use ($properties) { |
98
|
|
|
foreach ($properties as $property) { |
99
|
|
|
$getter = "get" . ucfirst($property); |
100
|
|
|
$object = $e->$getter(); |
101
|
|
|
if ($object === null) { |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
if ($object instanceof AbstractLazyCollection) { |
105
|
|
|
/** @var $object AbstractLazyCollection */ |
106
|
|
|
if (!$object->isInitialized()) { |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
} else if (!property_exists($object, '__isInitialized__')) { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
return false; |
114
|
|
|
})); |
115
|
|
|
if (count($ids) > 0) { |
116
|
|
|
$this->loadProperties($ids, $class, $properties); |
117
|
|
|
} |
118
|
|
|
//check loaded subproperties if they also have subproperties which need to get loaded |
119
|
|
|
foreach ($entities as $entity) { |
120
|
|
|
foreach ($properties as $property) { |
121
|
|
|
$getter = "get" . ucfirst($property); |
122
|
|
|
$object = $entity->$getter(); |
123
|
|
|
if ($object !== null) { |
124
|
|
|
if (!$object instanceof Collection) { |
125
|
|
|
$object = new ArrayCollection([$object]); |
126
|
|
|
} |
127
|
|
|
/** @var $object Collection|IdAble[] */ |
128
|
|
|
foreach ($object as $subObject) { |
129
|
|
|
if (!array_key_exists($subObject->getEntityId(), $done)) { |
130
|
|
|
$done[$subObject->getEntityId()] = true; |
131
|
|
|
$subClass = $this->keyOfPropertyMap($subObject, $propertyMap); |
|
|
|
|
132
|
|
|
if ($subClass !== null) { |
133
|
|
|
if (!array_key_exists($subClass, $toDoEntityIds)) { |
134
|
|
|
$toDoEntityIds[$subClass] = []; |
135
|
|
|
} |
136
|
|
|
$toDoEntityIds[$subClass][] = $subObject; |
137
|
|
|
break; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
//</editor-fold desc="Public Methods"> |
148
|
|
|
|
149
|
|
|
//<editor-fold desc="Private Methods"> |
150
|
|
|
/** |
151
|
|
|
* Computes the key corresponding to the given entity in the given property map, or null if no key was found. |
152
|
|
|
* @param mixed $entity the entity to search the key for |
153
|
|
|
* @param string[][][] $propertyMap a property map which maps classes to lists of groups of properties to fetch |
154
|
|
|
* @return null|string |
155
|
|
|
*/ |
156
|
|
|
private function keyOfPropertyMap($entity, $propertyMap): ?string |
157
|
|
|
{ |
158
|
|
|
foreach (array_keys($propertyMap) as $class) { |
159
|
|
|
if ($entity instanceof $class) { |
160
|
|
|
return $class; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string[] $ids a list of ids of entities to get the properties for |
168
|
|
|
* @param string $class the class of the entities |
169
|
|
|
* @param string[] $properties a list of properties to get for each entity |
170
|
|
|
*/ |
171
|
|
|
private function loadProperties(array $ids, string $class, array $properties): void |
172
|
|
|
{ |
173
|
|
|
$builder = $this->em->createQueryBuilder() |
174
|
|
|
->select("t1") |
175
|
|
|
->from($class, "t1"); |
176
|
|
|
$count = 1; |
177
|
|
|
foreach ($properties as $property) { |
178
|
|
|
$count++; |
179
|
|
|
$table = "t" . (string)$count; |
180
|
|
|
$builder->addSelect($table); |
181
|
|
|
$builder->leftJoin("t1." . $property, $table); |
182
|
|
|
} |
183
|
|
|
$builder->where($builder->expr()->in("t1.id", $ids)); |
184
|
|
|
$builder->getQuery()->getResult(); |
185
|
|
|
} |
186
|
|
|
//</editor-fold desc="Private Methods"> |
187
|
|
|
} |
188
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.