55use App \Repository \FileItemRepository ;
66use App \Services \UtilityService ;
77use Doctrine \ORM \Mapping as ORM ;
8-
8+ use Symfony \ Component \ Console \ Output \ ConsoleOutput ;
99
1010#[ORM \Entity(repositoryClass: FileItemRepository::class)]
1111class FileItem implements \JsonSerializable
@@ -139,16 +139,11 @@ public function setMetadata(?string $metadata): self
139139
140140 public function setReplacementFile ($ fileData ): self
141141 {
142- $ replacement = [
143- 'id ' => $ fileData ['id ' ],
144- 'fileName ' => $ fileData ['display_name ' ],
145- 'fileType ' => $ fileData ['mime_class ' ],
146- 'url ' => $ fileData ['url ' ],
147- ];
148-
149- $ tempMetadata = json_decode ($ this ->getMetadata (), true );
150- $ tempMetadata ['replacement ' ] = $ replacement ;
151- $ this ->setMetadata (json_encode ($ tempMetadata ));
142+ if (!isset ($ fileData ['id ' ])) {
143+ $ tempMetadata = json_decode ($ this ->getMetadata (), true );
144+ $ tempMetadata ['replacement ' ] = $ fileData ['id ' ];
145+ $ this ->setMetadata (json_encode ($ tempMetadata ));
146+ }
152147
153148 return $ this ;
154149 }
@@ -157,7 +152,21 @@ public function getReplacementFile(): ?array
157152 {
158153 $ metadata = json_decode ($ this ->getMetadata (), true );
159154 if (isset ($ metadata ['replacement ' ])) {
160- return $ metadata ['replacement ' ];
155+ // Fetch the replacement file from the database using the LMS File ID
156+ $ lmsFileId = $ metadata ['replacement ' ];
157+ $ output = new ConsoleOutput ();
158+ $ output ->writeln ("Fetching replacement file with LMS File ID: " . $ lmsFileId );
159+
160+ // TODO: Get ANOTHER file item IF it still exists in the database.
161+ // This file does NOT have access to the ManagerRegistry (doctrine) but we need it to do
162+ // something like this...
163+
164+ // $fileItemRepo = $this->doctrine->getManager()->getRepository(FileItem::class);
165+ // $replacementFile = $fileItemRepo->findOneBy(['lmsFileId' => $lmsFileId]);
166+
167+ // if ($replacementFile) {
168+ // return $replacementFile;
169+ // }
161170 }
162171
163172 return [];
@@ -232,6 +241,13 @@ public function update($file): self
232241
233242 public function jsonSerialize (): array
234243 {
244+ // If there is a replacement file, verify that it still exists.
245+ $ replacementFileData = $ this ->getReplacementFile ();
246+ if (isset ($ replacementFileData ['id ' ])) {
247+ $ output = new ConsoleOutput ();
248+ $ output ->writeln ("File Item " . $ this ->getId () . " has a replacement file ID of " . $ replacementFileData ['id ' ]);
249+ }
250+
235251 return [
236252 'id ' => $ this ->getId (),
237253 'fileName ' => $ this ->getFileName (),
@@ -246,6 +262,7 @@ public function jsonSerialize(): array
246262 'downloadUrl ' => $ this ->getDownloadUrl (),
247263 'lmsUrl ' => $ this ->getLmsUrl (),
248264 'metadata ' => json_decode ($ this ->getMetadata (), true ),
265+ 'replacement ' => $ replacementFileData ,
249266 ];
250267 }
251268
0 commit comments