1414namespace Silverback \ApiComponentsBundle \Helper \Uploadable ;
1515
1616use Doctrine \Common \Collections \ArrayCollection ;
17+ use Doctrine \ORM \Mapping \ClassMetadata ;
1718use Doctrine \Persistence \ManagerRegistry ;
1819use Liip \ImagineBundle \Service \FilterService ;
1920use Silverback \ApiComponentsBundle \Annotation \UploadableField ;
@@ -135,22 +136,18 @@ public function persistFiles(object $object): void
135136 if (!$ file ) {
136137 // so we need to know if it was a deleted field from the denormalizer
137138 if ($ this ->deletedFields ->contains ($ fieldConfiguration ->property )) {
138- // this will not have been updated yet, original database value - string file path
139- $ currentFilepath = $ classMetadata ->getFieldValue ($ object , $ fieldConfiguration ->property );
140- if ($ currentFilepath ) {
141- $ this ->removeFilepath ($ object , $ fieldConfiguration );
142- // file path set to null
143- $ classMetadata ->setFieldValue ($ object , $ fieldConfiguration ->property , null );
144- }
139+ $ this ->deleteFileForField ($ object , $ classMetadata , $ fieldConfiguration );
140+ $ classMetadata ->setFieldValue ($ object , $ fieldConfiguration ->property , null );
145141 }
146142 continue ;
147143 }
148144
145+ $ this ->deleteFileForField ($ object , $ classMetadata , $ fieldConfiguration );
149146 $ filesystem = $ this ->filesystemProvider ->getFilesystem ($ fieldConfiguration ->adapter );
150147
151148 $ path = $ fieldConfiguration ->prefix ?? '' ;
152149 $ path .= $ file ->getFilename ();
153- $ stream = fopen ($ file ->getRealPath (), 'r ' );
150+ $ stream = fopen ($ file ->getRealPath (), 'rb ' );
154151 $ filesystem ->writeStream (
155152 $ path ,
156153 $ stream ,
@@ -169,10 +166,15 @@ public function deleteFiles(object $object): void
169166
170167 $ configuredProperties = $ this ->annotationReader ->getConfiguredProperties ($ object , true );
171168 foreach ($ configuredProperties as $ fileProperty => $ fieldConfiguration ) {
172- $ currentFilepath = $ classMetadata ->getFieldValue ($ object , $ fieldConfiguration ->property );
173- if ($ currentFilepath ) {
174- $ this ->removeFilepath ($ object , $ fieldConfiguration );
175- }
169+ $ this ->deleteFileForField ($ object , $ classMetadata , $ fieldConfiguration );
170+ }
171+ }
172+
173+ private function deleteFileForField (object $ object , ClassMetadata $ classMetadata , UploadableField $ fieldConfiguration ): void
174+ {
175+ $ currentFilepath = $ classMetadata ->getFieldValue ($ object , $ fieldConfiguration ->property );
176+ if ($ currentFilepath ) {
177+ $ this ->removeFilepath ($ object , $ fieldConfiguration );
176178 }
177179 }
178180
0 commit comments