|
17 | 17 | use App\Repository\ScreenLayoutRegionsRepository; |
18 | 18 | use App\Repository\ScreenLayoutRepository; |
19 | 19 | use App\Utils\IriHelperUtils; |
| 20 | +use Doctrine\Common\Collections\ArrayCollection; |
20 | 21 | use Doctrine\ORM\EntityManagerInterface; |
21 | | -use Symfony\Component\Uid\Ulid; |
22 | 22 |
|
23 | 23 | class ScreenProcessor extends AbstractProcessor |
24 | 24 | { |
@@ -59,158 +59,61 @@ protected function fromInput(mixed $object, Operation $operation, array $uriVari |
59 | 59 | // Adding relations for playlist/screen/region |
60 | 60 | if (isset($object->regions) && isset($screen)) { |
61 | 61 | foreach ($object->regions as $regionAndPlaylists) { |
| 62 | + // Relevant region |
62 | 63 | $region = $this->screenLayoutRegionsRepository->findOneBy(['id' => $regionAndPlaylists['regionId']]); |
63 | 64 |
|
64 | 65 | if (is_null($region)) { |
65 | 66 | throw new InvalidArgumentException('Unknown region resource'); |
66 | 67 | } |
67 | 68 |
|
68 | | - $newPlaylistULIDs = array_map( |
69 | | - /** |
70 | | - * @param array<mixed> $playlistObject |
71 | | - * |
72 | | - * @return Ulid |
73 | | - */ |
74 | | - fn ($playlistObject): Ulid => Ulid::fromString($playlistObject['id']), $regionAndPlaylists['playlists']); |
75 | | - |
76 | | - $playlistScreens = $this->playlistScreenRegionRepository->findBy([ |
77 | | - 'screen' => $screen->getId(), |
78 | | - 'region' => $regionAndPlaylists['regionId'], |
79 | | - ]); |
80 | | - |
81 | | - $existingPlaylistsULIDs = array_map(function ($playlistObject) { |
82 | | - $playlist = $playlistObject->getPlaylist(); |
83 | | - if (!is_null($playlist)) { |
84 | | - return $playlist->getId(); |
85 | | - } |
86 | | - }, $playlistScreens); |
87 | | - |
88 | | - // This diff finds the playlists to be deleted |
89 | | - $deletePlaylistsULIDs = array_diff($existingPlaylistsULIDs, $newPlaylistULIDs); |
| 69 | + // Collection to be saved. |
| 70 | + $playlistScreenRegionCollection = new ArrayCollection(); |
90 | 71 |
|
91 | | - // ... and deletes them. |
92 | | - foreach ($deletePlaylistsULIDs as $deletePlaylist) { |
93 | | - $regionId = $region->getId(); |
94 | | - $screenId = $screen->getId(); |
95 | | - if (!is_null($screenId) && !is_null($regionId) && !is_null($deletePlaylist)) { |
96 | | - $this->playlistScreenRegionRepository->deleteRelations($screenId, $regionId, $deletePlaylist); |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - // This diff finds the playlists to be saved |
101 | | - $newPlaylistULIDs = array_diff($newPlaylistULIDs, $existingPlaylistsULIDs); |
102 | | - |
103 | | - // ... and saves them. |
104 | | - foreach ($newPlaylistULIDs as $newPlaylist) { |
105 | | - $playlistAndRegionToSave = new PlaylistScreenRegion(); |
106 | | - $playlist = $this->playlistRepository->findOneBy(['id' => $newPlaylist]); |
| 72 | + // Looping through playlists connected to region |
| 73 | + foreach ($regionAndPlaylists['playlists'] as $inputPlaylist) { |
| 74 | + // Checking if playlists exists |
| 75 | + $playlist = $this->playlistRepository->findOneBy(['id' => $inputPlaylist['id']]); |
107 | 76 |
|
108 | 77 | if (is_null($playlist)) { |
109 | 78 | throw new InvalidArgumentException('Unknown playlist resource'); |
110 | 79 | } |
111 | 80 |
|
112 | | - // Filter the array containing all the new playlists, to find the weight of the playlist currently |
113 | | - // set for save |
114 | | - $playlistWeight = array_filter($regionAndPlaylists['playlists'], |
115 | | - /** |
116 | | - * @param array<mixed> $playlistAndWeight |
117 | | - * |
118 | | - * @return bool |
119 | | - */ |
120 | | - fn ($playlistAndWeight) => Ulid::fromString($playlistAndWeight['id']) == $playlist->getId()); |
121 | | - |
122 | | - $playlistAndRegionToSave->setPlaylist($playlist); |
123 | | - $playlistAndRegionToSave->setRegion($region); |
124 | | - if (count($playlistWeight) > 0 && isset($playlistWeight[0]['weight'])) { |
125 | | - $playlistAndRegionToSave->setWeight($playlistWeight[0]['weight']); |
126 | | - } else { |
127 | | - $playlistAndRegionToSave->setWeight(0); |
128 | | - } |
129 | | - $screen->addPlaylistScreenRegion($playlistAndRegionToSave); |
130 | | - } |
131 | | - |
132 | | - $uneditedPlaylists = array_diff(array_diff($existingPlaylistsULIDs, $deletePlaylistsULIDs), $newPlaylistULIDs); |
133 | | - |
134 | | - foreach ($existingPlaylistsULIDs as $existingPlaylist) { |
135 | | - $region = $this->screenLayoutRegionsRepository->findOneBy(['id' => $regionAndPlaylists['regionId']]); |
136 | | - |
137 | | - if (is_null($region)) { |
138 | | - throw new InvalidArgumentException('Unknown region resource'); |
139 | | - } |
140 | | - |
141 | | - $playlist = $this->playlistRepository->findOneBy(['id' => $existingPlaylist]); |
142 | | - |
143 | | - if (is_null($playlist)) { |
144 | | - throw new InvalidArgumentException('Unknown playlist resource'); |
145 | | - } |
146 | | - |
147 | | - $psr = $this->playlistScreenRegionRepository->findOneBy([ |
148 | | - 'screen' => $screen->getId(), |
| 81 | + // See if relation already exists |
| 82 | + $existingPlaylistScreenRegion = $this->playlistScreenRegionRepository->findOneBy([ |
| 83 | + 'screen' => $screen, |
149 | 84 | 'region' => $region, |
150 | 85 | 'playlist' => $playlist, |
151 | 86 | ]); |
152 | 87 |
|
153 | | - // Filter the array containing all the new playlists, to find the weight of the playlist currently |
154 | | - // set for save |
155 | | - $playlistWeight = array_filter($regionAndPlaylists['playlists'], |
156 | | - /** |
157 | | - * @param array<mixed> $playlistAndWeight |
158 | | - * |
159 | | - * @return bool |
160 | | - */ |
161 | | - fn ($playlistAndWeight) => Ulid::fromString($playlistAndWeight['id']) == $existingPlaylist); |
162 | | - |
163 | | - if (count($playlistWeight) > 0) { |
164 | | - $psr->setWeight(reset($playlistWeight)['weight']); |
| 88 | + if (is_null($existingPlaylistScreenRegion)) { |
| 89 | + // If relation does not exist, create new PlaylistScreenRegion |
| 90 | + $newPlaylistScreenRegionRelation = new PlaylistScreenRegion(); |
| 91 | + $newPlaylistScreenRegionRelation->setPlaylist($playlist); |
| 92 | + $newPlaylistScreenRegionRelation->setRegion($region); |
| 93 | + $newPlaylistScreenRegionRelation->setScreen($screen); |
| 94 | + $newPlaylistScreenRegionRelation->setWeight($inputPlaylist['weight'] ?? 0); |
| 95 | + $playlistScreenRegionCollection->add($playlistAndRegionToSave); |
165 | 96 | } else { |
166 | | - $psr->setWeight(0); |
| 97 | + // Update weight, add existing relation |
| 98 | + $existingPlaylistScreenRegion->setWeight($inputPlaylist['weight'] ?? 0); |
| 99 | + $playlistScreenRegionCollection->add($existingPlaylistScreenRegion); |
167 | 100 | } |
168 | 101 | } |
| 102 | + $region->setPlaylistScreenRegions($playlistScreenRegionCollection); |
169 | 103 | } |
170 | 104 | } |
171 | 105 |
|
172 | 106 | // Maps ids of existing groups |
173 | 107 | if (isset($object->groups) && isset($screen)) { |
174 | | - $existingGroups = array_map(function ($group) { |
175 | | - if (!is_null($group)) { |
176 | | - return $group->getId(); |
177 | | - } |
178 | | - }, iterator_to_array($screen->getScreenGroups())); |
179 | | - |
180 | | - // Ids of groups inputted |
181 | | - $newGroupsId = array_map( |
182 | | - /** |
183 | | - * @param string $group |
184 | | - * |
185 | | - * @return Ulid |
186 | | - */ |
187 | | - fn ($group): Ulid => Ulid::fromString($group), $object->groups); |
188 | | - |
189 | | - // This diff finds the groups to be saved |
190 | | - $newGroups = array_diff($newGroupsId, $existingGroups); |
191 | | - // ... and saves them. |
192 | | - foreach ($newGroups as $group) { |
| 108 | + $groupCollection = new ArrayCollection(); |
| 109 | + foreach ($object->groups as $group) { |
193 | 110 | $groupToSave = $this->groupRepository->findOneBy(['id' => $group]); |
194 | | - |
195 | 111 | if (is_null($groupToSave)) { |
196 | | - throw new InvalidArgumentException('Unknown group resource'); |
| 112 | + throw new InvalidArgumentException('Unknown screen group resource'); |
197 | 113 | } |
198 | | - |
199 | | - $screen->addScreenGroup($groupToSave); |
200 | | - } |
201 | | - |
202 | | - // This diff finds the groups to be deleted |
203 | | - $deleteGroups = array_diff($existingGroups, $newGroupsId); |
204 | | - // ... and deletes them. |
205 | | - foreach ($deleteGroups as $group) { |
206 | | - $groupToDelete = $this->groupRepository->findOneBy(['id' => $group]); |
207 | | - |
208 | | - if (is_null($groupToDelete)) { |
209 | | - throw new InvalidArgumentException('Unknown group resource'); |
210 | | - } |
211 | | - |
212 | | - $screen->removeScreenGroup($groupToDelete); |
| 114 | + $groupCollection->add($groupToSave); |
213 | 115 | } |
| 116 | + $screen->setScreenGroups($groupCollection); |
214 | 117 | } |
215 | 118 |
|
216 | 119 | if (!empty($object->layout)) { |
|
0 commit comments