1616use DateTime ;
1717use DateTimeZone ;
1818use Display ;
19+ use DocumentManager ;
1920use Event ;
2021use Exception ;
2122use ExtraFieldValue ;
2425use MessageManager ;
2526use Symfony \Component \HttpFoundation \File \UploadedFile ;
2627use Template ;
28+ use Throwable ;
2729use UserManager ;
2830
2931use const PHP_ROUND_HALF_UP ;
@@ -696,26 +698,29 @@ public static function addLegacyFileToResource(
696698
697699 if (!self ::legacyFileUsable ($ filePath )) {
698700 error_log ("LEGACY_FILE: Cannot attach to {$ class } – file not found or unreadable: {$ basename }" );
701+
699702 return false ;
700703 }
701704
702705 // If the repository doesn't expose addFile(), use the Asset flow.
703706 if (!method_exists ($ repo , 'addFile ' )) {
704- error_log ("LEGACY_FILE: Repository " .get_class ($ repo )." has no addFile(), falling back to Asset flow " );
707+ error_log ('LEGACY_FILE: Repository ' .$ repo ::class.' has no addFile(), falling back to Asset flow ' );
708+
705709 return self ::attachLegacyFileToResource ($ filePath , $ resource , $ fileName );
706710 }
707711
708712 try {
709713 $ mimeType = self ::legacyDetectMime ($ filePath );
710- $ finalName = $ fileName !== '' ? $ fileName : $ basename ;
714+ $ finalName = '' !== $ fileName ? $ fileName : $ basename ;
711715
712716 // UploadedFile in "test mode" (last arg true) avoids PHP upload checks.
713717 $ uploaded = new UploadedFile ($ filePath , $ finalName , $ mimeType , null , true );
714718 $ repo ->addFile ($ resource , $ uploaded , $ description );
715719
716720 return true ;
717- } catch (\Throwable $ e ) {
718- error_log ("LEGACY_FILE EXCEPTION (addFile): " .$ e ->getMessage ());
721+ } catch (Throwable $ e ) {
722+ error_log ('LEGACY_FILE EXCEPTION (addFile): ' .$ e ->getMessage ());
723+
719724 return false ;
720725 }
721726 }
@@ -736,11 +741,13 @@ public static function attachLegacyFileToResource(
736741
737742 if (!self ::legacyFileUsable ($ filePath )) {
738743 error_log ("LEGACY_FILE: Cannot attach Asset to {$ class } – file not found or unreadable: {$ basename }" );
744+
739745 return false ;
740746 }
741747
742748 if (!method_exists ($ resource , 'getResourceNode ' ) || null === $ resource ->getResourceNode ()) {
743749 error_log ("LEGACY_FILE: Resource has no ResourceNode – cannot attach Asset (class: {$ class }) " );
750+
744751 return false ;
745752 }
746753
@@ -751,22 +758,23 @@ public static function attachLegacyFileToResource(
751758 if (method_exists ($ assetRepo , 'createFromLocalPath ' )) {
752759 $ asset = $ assetRepo ->createFromLocalPath (
753760 $ filePath ,
754- $ fileName !== '' ? $ fileName : $ basename
761+ '' !== $ fileName ? $ fileName : $ basename
755762 );
756763 } else {
757764 // Fallback: simulate an upload-like array for createFromRequest().
758765 $ mimeType = self ::legacyDetectMime ($ filePath );
759766 $ fakeUpload = [
760767 'tmp_name ' => $ filePath ,
761- 'name ' => $ fileName !== '' ? $ fileName : $ basename ,
762- 'type ' => $ mimeType ,
763- 'size ' => @filesize ($ filePath ) ?: null ,
764- 'error ' => 0 ,
768+ 'name ' => '' !== $ fileName ? $ fileName : $ basename ,
769+ 'type ' => $ mimeType ,
770+ 'size ' => @filesize ($ filePath ) ?: null ,
771+ 'error ' => 0 ,
765772 ];
766773
767774 $ asset = (new Asset ())
768775 ->setTitle ($ fakeUpload ['name ' ])
769- ->setCompressed (false );
776+ ->setCompressed (false )
777+ ;
770778
771779 // AssetRepository::createFromRequest(Asset $asset, array $uploadLike)
772780 $ assetRepo ->createFromRequest ($ asset , $ fakeUpload );
@@ -775,20 +783,24 @@ public static function attachLegacyFileToResource(
775783 // Attach to the resource's node.
776784 if (method_exists ($ assetRepo , 'attachToNode ' )) {
777785 $ assetRepo ->attachToNode ($ asset , $ resource ->getResourceNode ());
786+
778787 return true ;
779788 }
780789
781790 // If the resource repository exposes a direct helper:
782791 $ repo = self ::guessResourceRepository ($ resource );
783792 if ($ repo && method_exists ($ repo , 'attachAssetToResource ' )) {
784793 $ repo ->attachAssetToResource ($ resource , $ asset );
794+
785795 return true ;
786796 }
787797
788- error_log ("LEGACY_FILE: No method to attach Asset to node (missing attachToNode/attachAssetToResource) " );
798+ error_log ('LEGACY_FILE: No method to attach Asset to node (missing attachToNode/attachAssetToResource) ' );
799+
789800 return false ;
790- } catch (\Throwable $ e ) {
791- error_log ("LEGACY_FILE EXCEPTION (Asset attach): " .$ e ->getMessage ());
801+ } catch (Throwable $ e ) {
802+ error_log ('LEGACY_FILE EXCEPTION (Asset attach): ' .$ e ->getMessage ());
803+
792804 return false ;
793805 }
794806 }
@@ -804,6 +816,7 @@ public static function firstExistingPath(array $candidates): ?string
804816 return $ p ;
805817 }
806818 }
819+
807820 return null ;
808821 }
809822
@@ -815,6 +828,7 @@ private static function legacyFileUsable(string $filePath): bool
815828 private static function legacyDetectMime (string $ filePath ): string
816829 {
817830 $ mime = @mime_content_type ($ filePath );
831+
818832 return $ mime ?: 'application/octet-stream ' ;
819833 }
820834
@@ -825,10 +839,11 @@ private static function legacyDetectMime(string $filePath): string
825839 private static function guessResourceRepository (AbstractResource $ resource ): ?ResourceRepository
826840 {
827841 try {
828- $ em = \Database::getManager ();
829- $ repo = $ em ->getRepository (get_class ($ resource ));
842+ $ em = Database::getManager ();
843+ $ repo = $ em ->getRepository ($ resource ::class);
844+
830845 return $ repo instanceof ResourceRepository ? $ repo : null ;
831- } catch (\ Throwable $ e ) {
846+ } catch (Throwable $ e ) {
832847 return null ;
833848 }
834849 }
@@ -842,7 +857,7 @@ public static function attachLegacyFileWithPublicUrl(
842857 AbstractResource $ ownerResource ,
843858 string $ fileName = ''
844859 ): array {
845- $ basename = $ fileName !== '' ? $ fileName : basename ($ filePath );
860+ $ basename = '' !== $ fileName ? $ fileName : basename ($ filePath );
846861
847862 if (!self ::legacyFileUsable ($ filePath )) {
848863 return ['ok ' => false , 'asset ' => null , 'url ' => null , 'error ' => "File not found or unreadable: $ basename " ];
@@ -858,25 +873,25 @@ public static function attachLegacyFileWithPublicUrl(
858873 $ mimeType = self ::legacyDetectMime ($ filePath );
859874 $ fakeUpload = [
860875 'tmp_name ' => $ filePath ,
861- 'name ' => $ basename ,
862- 'type ' => $ mimeType ,
863- 'size ' => @filesize ($ filePath ) ?: null ,
864- 'error ' => 0 ,
876+ 'name ' => $ basename ,
877+ 'type ' => $ mimeType ,
878+ 'size ' => @filesize ($ filePath ) ?: null ,
879+ 'error ' => 0 ,
865880 ];
866881 $ asset = (new Asset ())->setTitle ($ basename )->setCompressed (false );
867882 $ assetRepo ->createFromRequest ($ asset , $ fakeUpload );
868883 }
869884
870885 if (!method_exists ($ ownerResource , 'getResourceNode ' ) || null === $ ownerResource ->getResourceNode ()) {
871- return ['ok ' => false , 'asset ' => null , 'url ' => null , 'error ' => " Owner resource has no ResourceNode " ];
886+ return ['ok ' => false , 'asset ' => null , 'url ' => null , 'error ' => ' Owner resource has no ResourceNode ' ];
872887 }
873888
874889 if (method_exists ($ assetRepo , 'attachToNode ' )) {
875890 $ assetRepo ->attachToNode ($ asset , $ ownerResource ->getResourceNode ());
876891 } else {
877892 $ repo = self ::guessResourceRepository ($ ownerResource );
878893 if (!$ repo || !method_exists ($ repo , 'attachAssetToResource ' )) {
879- return ['ok ' => false , 'asset ' => $ asset , 'url ' => null , 'error ' => " No way to attach asset to node " ];
894+ return ['ok ' => false , 'asset ' => $ asset , 'url ' => null , 'error ' => ' No way to attach asset to node ' ];
880895 }
881896 $ repo ->attachAssetToResource ($ ownerResource , $ asset );
882897 }
@@ -890,8 +905,7 @@ public static function attachLegacyFileWithPublicUrl(
890905 }
891906
892907 return ['ok ' => true , 'asset ' => $ asset , 'url ' => $ url , 'error ' => null ];
893-
894- } catch (\Throwable $ e ) {
908+ } catch (Throwable $ e ) {
895909 return ['ok ' => false , 'asset ' => null , 'url ' => null , 'error ' => $ e ->getMessage ()];
896910 }
897911 }
@@ -906,11 +920,11 @@ public static function rewriteLegacyCourseUrlsToAssets(
906920 string $ backupRoot ,
907921 array $ extraRoots = []
908922 ): string {
909- if ($ html === '' ) {
923+ if ('' === $ html ) {
910924 return $ html ;
911925 }
912926
913- $ sources = \ DocumentManager::get_resources_from_source_html ($ html , false , TOOL_DOCUMENT , 1 );
927+ $ sources = DocumentManager::get_resources_from_source_html ($ html , false , TOOL_DOCUMENT , 1 );
914928 if (empty ($ sources )) {
915929 return $ html ;
916930 }
@@ -919,16 +933,16 @@ public static function rewriteLegacyCourseUrlsToAssets(
919933
920934 foreach ($ sources as $ s ) {
921935 [$ realUrl , $ scope , $ kind ] = $ s ;
922- if ($ scope !== ' local ' ) {
936+ if (' local ' !== $ scope ) {
923937 continue ;
924938 }
925939
926940 $ pos = strpos ($ realUrl , 'document/ ' );
927- if ($ pos === false ) {
941+ if (false === $ pos ) {
928942 continue ;
929943 }
930944
931- $ relAfterDocument = ltrim (substr ($ realUrl , $ pos + strlen ('document/ ' )), '/ ' );
945+ $ relAfterDocument = ltrim (substr ($ realUrl , $ pos + \ strlen ('document/ ' )), '/ ' );
932946
933947 $ candidates = [];
934948 foreach ($ roots as $ root ) {
@@ -944,7 +958,8 @@ public static function rewriteLegacyCourseUrlsToAssets(
944958
945959 $ attached = self ::attachLegacyFileWithPublicUrl ($ filePath , $ ownerResource , basename ($ filePath ));
946960 if (!$ attached ['ok ' ] || empty ($ attached ['url ' ])) {
947- error_log ("LEGACY_REWRITE: failed for $ realUrl ( " .$ attached ['error ' ].") " );
961+ error_log ("LEGACY_REWRITE: failed for $ realUrl ( " .$ attached ['error ' ].') ' );
962+
948963 continue ;
949964 }
950965
0 commit comments