Skip to content

Commit c98d07a

Browse files
author
Sine Jespersen
committed
2314: comment on pr, move function to entity instead of processor
1 parent 5c6ccf2 commit c98d07a

File tree

4 files changed

+55
-128
lines changed

4 files changed

+55
-128
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ All notable changes to this project will be documented in this file.
1010
- [#213](https://github.com/os2display/display-api-service/pull/213)
1111
- Add regions and groups to `ScreenInput.php`
1212
- Add "cascade: persist remove" to PlaylistScreenRegion
13-
- Save playlist/regions in `ScreenProcessor.php` (as an alternative to sending multiple requests)
14-
- Save groups in `ScreenProcessor.php`
13+
- Save playlist/regions in `ScreenProcessor.php` and in `src/entity/ScreenLayoutRegions` (as an alternative to sending multiple requests)
14+
- Save groups in `ScreenProcessor.php` and in `src/entity/tenant/Screen.php`
1515
- Update psalm baseline
1616
- Add regions/playlists and groups to POST screen test
1717
- `composer update symfony/* --with-dependencies`

src/Entity/ScreenLayoutRegions.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ScreenLayoutRegions extends AbstractBaseEntity implements MultiTenantInter
4141
/**
4242
* @var \Doctrine\Common\Collections\Collection<int, \App\Entity\Tenant\PlaylistScreenRegion>|\App\Entity\Tenant\PlaylistScreenRegion[]
4343
*/
44-
#[ORM\OneToMany(targetEntity: PlaylistScreenRegion::class, mappedBy: 'region', orphanRemoval: true)]
44+
#[ORM\OneToMany(targetEntity: PlaylistScreenRegion::class, cascade: ['persist', 'remove'], mappedBy: 'region', orphanRemoval: true)]
4545
private Collection $playlistScreenRegions;
4646

4747
public function __construct()
@@ -82,6 +82,18 @@ public function getPlaylistScreenRegions(): Collection
8282
return $this->playlistScreenRegions;
8383
}
8484

85+
public function setPlaylistScreenRegions(Collection $playlistScreenRegions): void
86+
{
87+
foreach ($this->playlistScreenRegions as $playlistScreenRegion) {
88+
if (false === $playlistScreenRegions->contains($playlistScreenRegion)) {
89+
$this->removePlaylistScreenRegion($playlistScreenRegion);
90+
}
91+
}
92+
foreach ($playlistScreenRegions as $playlistScreenRegion) {
93+
$this->addPlaylistScreenRegion($playlistScreenRegion);
94+
}
95+
}
96+
8597
public function addPlaylistScreenRegion(PlaylistScreenRegion $playlistScreenRegion): self
8698
{
8799
if (!$this->playlistScreenRegions->contains($playlistScreenRegion)) {

src/Entity/Tenant/Screen.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ public function removeAllPlaylistScreenRegions(): self
173173
return $this;
174174
}
175175

176+
public function setScreenGroups(Collection $screenGroups): void
177+
{
178+
foreach ($this->screenGroups as $screenGroup) {
179+
if (false === $screenGroups->contains($screenGroup)) {
180+
$this->removeScreenGroup($screenGroup);
181+
}
182+
}
183+
foreach ($screenGroups as $screenGroup) {
184+
$this->addScreenGroup($screenGroup);
185+
}
186+
}
187+
176188
/**
177189
* @return Collection
178190
*/

src/State/ScreenProcessor.php

Lines changed: 28 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use App\Repository\ScreenLayoutRegionsRepository;
1818
use App\Repository\ScreenLayoutRepository;
1919
use App\Utils\IriHelperUtils;
20+
use Doctrine\Common\Collections\ArrayCollection;
2021
use Doctrine\ORM\EntityManagerInterface;
21-
use Symfony\Component\Uid\Ulid;
2222

2323
class ScreenProcessor extends AbstractProcessor
2424
{
@@ -59,158 +59,61 @@ protected function fromInput(mixed $object, Operation $operation, array $uriVari
5959
// Adding relations for playlist/screen/region
6060
if (isset($object->regions) && isset($screen)) {
6161
foreach ($object->regions as $regionAndPlaylists) {
62+
// Relevant region
6263
$region = $this->screenLayoutRegionsRepository->findOneBy(['id' => $regionAndPlaylists['regionId']]);
6364

6465
if (is_null($region)) {
6566
throw new InvalidArgumentException('Unknown region resource');
6667
}
6768

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();
9071

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']]);
10776

10877
if (is_null($playlist)) {
10978
throw new InvalidArgumentException('Unknown playlist resource');
11079
}
11180

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,
14984
'region' => $region,
15085
'playlist' => $playlist,
15186
]);
15287

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);
16596
} else {
166-
$psr->setWeight(0);
97+
// Update weight, add existing relation
98+
$existingPlaylistScreenRegion->setWeight($inputPlaylist['weight'] ?? 0);
99+
$playlistScreenRegionCollection->add($existingPlaylistScreenRegion);
167100
}
168101
}
102+
$region->setPlaylistScreenRegions($playlistScreenRegionCollection);
169103
}
170104
}
171105

172106
// Maps ids of existing groups
173107
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) {
193110
$groupToSave = $this->groupRepository->findOneBy(['id' => $group]);
194-
195111
if (is_null($groupToSave)) {
196-
throw new InvalidArgumentException('Unknown group resource');
112+
throw new InvalidArgumentException('Unknown screen group resource');
197113
}
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);
213115
}
116+
$screen->setScreenGroups($groupCollection);
214117
}
215118

216119
if (!empty($object->layout)) {

0 commit comments

Comments
 (0)