88use ApiPlatform \Metadata \Operation ;
99use ApiPlatform \State \ProcessorInterface ;
1010use App \Dto \ScreenInput ;
11+ use App \Entity \Tenant \Playlist ;
1112use App \Entity \Tenant \PlaylistScreenRegion ;
1213use App \Entity \Tenant \Screen ;
1314use App \Repository \PlaylistRepository ;
@@ -64,25 +65,36 @@ protected function fromInput(mixed $object, Operation $operation, array $uriVari
6465 throw new InvalidArgumentException ('Unknown region resource ' );
6566 }
6667
67- $ newPlaylists = array_map (function ($ playlistObject ) {
68- return Ulid::fromString ($ playlistObject ['id ' ]);
69- }, $ regionAndPlaylists ['playlists ' ]);
68+ $ newPlaylists = array_map (
69+ /**
70+ * @param array<mixed> $playlistObject
71+ *
72+ * @return Ulid
73+ */
74+ fn ($ playlistObject ): Ulid => Ulid::fromString ($ playlistObject ['id ' ]), $ regionAndPlaylists ['playlists ' ]);
7075
7176 $ playlistScreens = $ this ->playlistScreenRegionRepository ->findBy ([
7277 'screen ' => $ screen ->getId (),
7378 'region ' => $ regionAndPlaylists ['regionId ' ],
7479 ]);
7580
7681 $ existingPlaylists = array_map (function ($ playlistObject ) {
77- return $ playlistObject ->getPlaylist ()->getId ();
82+ $ playlist = $ playlistObject ->getPlaylist ();
83+ if (!is_null ($ playlist )) {
84+ return $ playlist ->getId ();
85+ }
7886 }, $ playlistScreens );
7987
8088 // This diff finds the playlists to be deleted
8189 $ deletePlaylists = array_diff ($ existingPlaylists , $ newPlaylists );
8290
8391 // ... and deletes them.
8492 foreach ($ deletePlaylists as $ deletePlaylist ) {
85- $ playlistScreens = $ this ->playlistScreenRegionRepository ->deleteRelations ($ screen ->getId (), $ region ->getId (), $ 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+ }
8698 }
8799
88100 // This diff finds the playlists to be saved
@@ -97,7 +109,14 @@ protected function fromInput(mixed $object, Operation $operation, array $uriVari
97109 throw new InvalidArgumentException ('Unknown playlist resource ' );
98110 }
99111
100- $ playlistWeight = array_filter ($ regionAndPlaylists ['playlists ' ], fn ($ playlistAndWeight ) => Ulid::fromString ($ playlistAndWeight ['id ' ]) == $ playlist ->getId ());
112+ $ playlistWeight = array_filter ($ regionAndPlaylists ['playlists ' ],
113+ /**
114+ * @param array<mixed> $playlistAndWeight
115+ *
116+ * @return bool
117+ */
118+ fn ($ playlistAndWeight ) => Ulid::fromString ($ playlistAndWeight ['id ' ]) == $ playlist ->getId ());
119+
101120 $ playlistAndRegionToSave ->setPlaylist ($ playlist );
102121 $ playlistAndRegionToSave ->setRegion ($ region );
103122 $ playlistAndRegionToSave ->setWeight ($ playlistWeight [0 ]['weight ' ]);
0 commit comments