diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..dc5728c --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,14 @@ +name: CI + +on: [push] + +jobs: + build-test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - uses: php-actions/composer@v6 + - uses: php-actions/phpstan@v3 + with: + memory_limit: -1 diff --git a/Command/H5pBundleCleanUpFilesCommand.php b/Command/H5pBundleCleanUpFilesCommand.php index b2b07df..fd9b82d 100644 --- a/Command/H5pBundleCleanUpFilesCommand.php +++ b/Command/H5pBundleCleanUpFilesCommand.php @@ -2,15 +2,26 @@ namespace Studit\H5PBundle\Command; +use Studit\H5PBundle\Core\H5POptions; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Style\SymfonyStyle; class H5pBundleCleanUpFilesCommand extends Command { + + /** + * @var H5POptions $h5POptions + */ + private $h5POptions; + + public function __construct(H5POptions $h5POptions) + { + $this->h5POptions = $h5POptions; + parent::__construct(); + } + protected static $defaultName = 'h5p-bundle:cleanup-files'; protected function configure() { @@ -31,7 +42,7 @@ private function cleanupFiles(InputInterface $input) { $location = $input->getArgument('location'); if (!$location) { - $location = $this->get('studit_h5p.options')->getAbsoluteH5PPath() . '/editor'; + $location = $this->h5POptions->getAbsoluteH5PPath() . '/editor'; } \H5PCore::deleteFileTree($location); } diff --git a/Controller/H5PAJAXController.php b/Controller/H5PAJAXController.php index 390ed6b..9bc7e82 100644 --- a/Controller/H5PAJAXController.php +++ b/Controller/H5PAJAXController.php @@ -94,11 +94,9 @@ public function TranslationsCallback(Request $request) /** * Callback Install library from external file * - * @param string $token Security token - * @param int $content_id Id of content - * @param string $machine_name Machine name of library * @param Request $request * + * @return JsonResponse * @Route("/library-install/") */ public function libraryInstallCallback(Request $request) @@ -121,11 +119,8 @@ public function libraryInstallCallback(Request $request) /** * Callback that returns data for a given library * - * @param string $machine_name Machine name of library - * @param int $major_version Major version of library - * @param int $minor_version Minor version of library - * @param string $locale Language of your website and plugins for default is English (EN) * @param Request $request + * @return JsonResponse */ private function libraryCallback(Request $request) { @@ -140,7 +135,7 @@ private function libraryCallback(Request $request) $request->get('majorVersion'), $request->get('minorVersion'), $locale, - $this->get('studit_h5p.options')->getOption('storage_dir'), + $this->serviceh5poptions->getOption('storage_dir'), '', $locale ); @@ -158,10 +153,9 @@ private function libraryCallback(Request $request) /** * Callback for uploading a library * - * @param string $token Editor security token - * @param int $content_id Id of content that is being edited * @param Request $request * + * @return JsonResponse * @throws Exception * @Route("/library-upload/") */ @@ -194,9 +188,8 @@ public function libraryUploadCallback(Request $request) /** * Callback for file uploads. * - * @param string $token SecuritlibraryCallbacky token - * @param int $content_id Content id * @param Request $request + * @return JsonResponse * @Route("/files/") */ public function filesCallback(Request $request) @@ -220,9 +213,8 @@ public function filesCallback(Request $request) /** * Callback for filtering. * - * @param string $token Security token - * @param int $content_id Content id * @param Request $request + * @return JsonResponse * @Route("/filter/") */ public function filterCallback(Request $request) diff --git a/Controller/H5PController.php b/Controller/H5PController.php index 57e9042..4f173c7 100644 --- a/Controller/H5PController.php +++ b/Controller/H5PController.php @@ -3,6 +3,7 @@ namespace Studit\H5PBundle\Controller; +use Doctrine\ORM\EntityManagerInterface; use Studit\H5PBundle\Core\H5POptions; use Studit\H5PBundle\Editor\LibraryStorage; use Studit\H5PBundle\Entity\Content; @@ -22,13 +23,16 @@ class H5PController extends AbstractController protected $h5PIntegrations; protected $libraryStorage; + protected $entityManager; public function __construct( H5PIntegration $h5PIntegration, - LibraryStorage $libraryStorage + LibraryStorage $libraryStorage, + EntityManagerInterface $entityManager ) { $this->h5PIntegrations = $h5PIntegration; $this->libraryStorage = $libraryStorage; + $this->entityManager = $entityManager; } /** @@ -37,7 +41,7 @@ public function __construct( */ public function listAction() { - $contents = $this->getDoctrine()->getRepository('Studit\H5PBundle\Entity\Content')->findAll(); + $contents = $this->entityManager->getRepository('Studit\H5PBundle\Entity\Content')->findAll(); return $this->render('@StuditH5P/list.html.twig', ['contents' => $contents]); } /** @@ -78,11 +82,12 @@ public function newAction(Request $request) { return $this->handleRequest($request ); } + /** * @Route("edit/{content}") * @param Request $request * @param Content $content - * @return + * @return RedirectResponse|Response */ public function editAction(Request $request, Content $content) { diff --git a/Controller/H5PInteractionController.php b/Controller/H5PInteractionController.php index c75fb29..259a937 100644 --- a/Controller/H5PInteractionController.php +++ b/Controller/H5PInteractionController.php @@ -4,20 +4,50 @@ namespace Studit\H5PBundle\Controller; +use Doctrine\ORM\EntityManagerInterface; use Exception; +use H5PCore; +use Studit\H5PBundle\Core\H5PIntegration; +use Studit\H5PBundle\Core\H5POptions; use Studit\H5PBundle\Entity\Content; use Studit\H5PBundle\Entity\ContentUserData; use Studit\H5PBundle\Service\ResultService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\Asset\Packages; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Component\Serializer\SerializerInterface; /** * @Route("/h5p/interaction") */ class H5PInteractionController extends AbstractController{ + + protected $entityManager; + protected $resultService; + protected $serializer; + protected $assetsPaths; + protected $options; + protected $h5PIntegration; + protected $h5PCore; + protected $kernel; + + public function __construct(EntityManagerInterface $entityManager, ResultService $resultService, SerializerInterface $serializer, Packages $packages, H5POptions $options, H5PIntegration $h5PIntegration, H5PCore $h5PCore, KernelInterface $kernel) + { + $this->entityManager = $entityManager; + $this->resultService = $resultService; + $this->serializer = $serializer; + $this->assetsPaths = $packages; + $this->options = $options; + $this->h5PIntegration = $h5PIntegration; + $this->h5PCore = $h5PCore; + $this->kernel = $kernel; + } + /** * Access callback for the setFinished feature * @@ -32,9 +62,9 @@ public function setFinished(Request $request, $token) \H5PCore::ajaxError('Invalid security token'); } /* @var ResultService $rs */ - $rs = $this->get('studit_h5p.result_storage'); - $result = $rs->handleRequestFinished($request, $this->getUser()->getId()); - $em = $this->getDoctrine()->getManager(); + $rs = $this->resultService; + $result = $rs->handleRequestFinished($request, $this->getUserId($this->getUser())); + $em = $this->entityManager; $em->persist($result); $em->flush(); return new JsonResponse(['success' => true]); @@ -61,7 +91,7 @@ public function contentUserData(Request $request, $contentId, $dataType, $subCon $data = $request->get("data"); $preload = $request->get("preload"); $invalidate = $request->get("invalidate"); - $em = $this->getDoctrine()->getManager(); + $em = $this->entityManager; if ($data !== NULL && $preload !== NULL && $invalidate !== NULL) { if(!\H5PCore::validToken('contentuserdata', $request->get("token"))){ return new JsonResponse(['success' => false, 'message' => 'No content']); @@ -70,7 +100,7 @@ public function contentUserData(Request $request, $contentId, $dataType, $subCon if ($data === '0'){ //remove data here /* @var ResultService $rs */ - $rs = $this->get('studit_h5p.result_storage'); + $rs = $this->resultService; $rs->removeData($contentId, $dataType, $user, $subContentId); }else{ // Wash values to ensure 0 or 1. @@ -85,7 +115,7 @@ public function contentUserData(Request $request, $contentId, $dataType, $subCon 'subContentId' => $subContentId, 'mainContent' => $contentId, 'dataId' => $dataType, - 'user' => $user->getId() + 'user' => $this->getUserId($user), ] ); if(!$update){ @@ -94,16 +124,14 @@ public function contentUserData(Request $request, $contentId, $dataType, $subCon * @var ContentUserData $contentUserData */ $contentUserData = new ContentUserData(); - $contentUserData->setUser($user->getId()); + $contentUserData->setUser($this->getUserId($user)); $contentUserData->setData($data); $contentUserData->setDataId($dataType); $contentUserData->setSubContentId($subContentId); $contentUserData->setPreloaded($preload); $contentUserData->setDeleteOnContentChange($invalidate); $contentUserData->setTimestamp( time ()); - /** - * @var $content Content - */ + /** @var Content|null $content */ $content = $em->getRepository('Studit\H5PBundle\Entity\Content')->findOneBy(['id' => $contentId]); $contentUserData->setMainContent($content); $em->persist($contentUserData); @@ -126,14 +154,14 @@ public function contentUserData(Request $request, $contentId, $dataType, $subCon 'subContentId' => $subContentId, 'mainContent' => $contentId, 'dataId' => $dataType, - 'user' => $user->getId() + 'user' => $this->getUserId($user), ] ); //decode for read the information return new JsonResponse([ 'success' => true, - 'data' => json_decode($this->get('serializer')->serialize($data, 'json')), + 'data' => json_decode($this->serializer->serialize($data, 'json')), ]); } } @@ -154,18 +182,13 @@ public function embedAction(Request $request, Content $content) ], ]; $h5p_content = $content; - if (empty($h5p_content)){ - //change url here - $response['#markup'] = '
' . t('Content unavailable.') . '
'; - return new Response($response['#markup']); - } // Grab the core integration settings - $integration = $this->get('studit_h5p.integration')->getGenericH5PIntegrationSettings(); + $integration = $this->h5PIntegration->getGenericH5PIntegrationSettings(); $content_id_string = 'cid-' . $content->getId(); // Add content specific settings - $integration['contents'][$content_id_string] = $this->get('studit_h5p.integration')->getH5PContentIntegrationSettings($content); - $preloaded_dependencies = $this->get('studit_h5p.core')->loadContentDependencies($content->getId(), 'preloaded'); - $files = $this->get('studit_h5p.core')->getDependenciesFiles($preloaded_dependencies, $this->get('studit_h5p.options')->getRelativeH5PPath()); + $integration['contents'][$content_id_string] = $this->h5PIntegration->getH5PContentIntegrationSettings($content); + $preloaded_dependencies = $this->h5PCore->loadContentDependencies($content->getId(), 'preloaded'); + $files = $this->h5PCore->getDependenciesFiles($preloaded_dependencies, $this->options->getRelativeH5PPath()); // Load public files $jsFilePaths = array_map(function ($asset) { return $asset->path; @@ -174,7 +197,7 @@ public function embedAction(Request $request, Content $content) return $asset->path; }, $files['styles']); // Load core assets - $coreAssets = $this->get('studit_h5p.integration')->getCoreAssets(); + $coreAssets = $this->h5PIntegration->getCoreAssets(); // Merge assets $scripts = array_merge($coreAssets['scripts'], $jsFilePaths); $styles = array_merge($cssFilePaths, $coreAssets['styles']); @@ -187,9 +210,22 @@ public function embedAction(Request $request, Content $content) 'title' => "H5P Content {$id}", ]; //include the embed file (provide in h5p-core) - include $this->get('kernel')->getProjectDir().'/vendor/h5p/h5p-core/embed.php'; + include $this->kernel->getProjectDir().'/vendor/h5p/h5p-core/embed.php'; $response['#markup'] = ob_get_clean(); //return nes Response HTML return new Response($response['#markup']); } + + private function getH5PAssetUrl() + { + return $this->assetsPaths->getUrl($this->options->getH5PAssetPath()); + } + + private function getUserId(UserInterface $user) + { + if (method_exists($user, 'getId')) { + return $user->getId(); + } + return $user->getUserIdentifier(); + } } \ No newline at end of file diff --git a/Core/H5PIntegration.php b/Core/H5PIntegration.php index b5f191b..451aaa6 100644 --- a/Core/H5PIntegration.php +++ b/Core/H5PIntegration.php @@ -5,13 +5,15 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; +use H5PCore; +use H5peditor; use Studit\H5PBundle\Entity\Content; use Symfony\Component\Asset\Packages; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; - +use Symfony\Component\Security\Core\User\UserInterface; class H5PIntegration @@ -108,8 +110,8 @@ public function getGenericH5PIntegrationSettings() ); if (is_object($user)) { $settings['user'] = [ - 'name' => $user->getUsername(), - 'mail' => $user->getEmail(), + 'name' => method_exists($user, 'getUsername') ? $user->getUsername() : $user->getUserIdentifier(), + 'mail' => method_exists($user, 'getEmail') ? $user->getEmail() : $user->getUserIdentifier().'@'.$_SERVER['HTTP_HOST'], ]; } return $settings; @@ -117,10 +119,10 @@ public function getGenericH5PIntegrationSettings() /** * Get a list with prepared asset links that is used when JS loads components. * - * @param array [$keys] Optional keys, first for JS second for CSS. + * @param null|array $keys [$keys] Optional keys, first for JS second for CSS. * @return array */ - public function getCoreAssets($keys = NULL) + public function getCoreAssets($keys = null) { if (empty($keys)) { $keys = ['scripts', 'styles']; @@ -148,7 +150,7 @@ public function getH5PContentIntegrationSettings(Content $content) ] ]; if (is_object($this->tokenStorage->getToken()->getUser())) { - $contentUserData = $this->entityManager->getRepository('Studit\H5PBundle\Entity\ContentUserData')->findOneBy(['mainContent' => $content, 'user' => $this->tokenStorage->getToken()->getUser()->getId()]); + $contentUserData = $this->entityManager->getRepository('Studit\H5PBundle\Entity\ContentUserData')->findOneBy(['mainContent' => $content, 'user' => $this->getUserId($this->tokenStorage->getToken()->getUser())]); if ($contentUserData) { $content_user_data[$contentUserData->getSubContentId()][$contentUserData->getDataId()] = $contentUserData->getData(); } @@ -232,12 +234,12 @@ private function getEditorAssets() $corePath = "{$h5pAssetUrl}/h5p-core/"; $editorPath = "{$h5pAssetUrl}/h5p-editor/"; $css = array_merge( - $this->getAssets(\H5PCore::$styles, $corePath), - $this->getAssets(\H5PEditor::$styles, $editorPath) + $this->getAssets(H5PCore::$styles, $corePath), + $this->getAssets(H5peditor::$styles, $editorPath) ); $js = array_merge( - $this->getAssets(\H5PCore::$scripts, $corePath), - $this->getAssets(\H5PEditor::$scripts, $editorPath, ['scripts/h5peditor-editor.js']) + $this->getAssets(H5PCore::$scripts, $corePath), + $this->getAssets(H5PEditor::$scripts, $editorPath, ['scripts/h5peditor-editor.js']) ); $js[] = $this->getTranslationFilePath(); return ['css' => $css, 'js' => $js]; @@ -305,4 +307,12 @@ public function getOptions() { return $this->options; } + + private function getUserId(UserInterface $user) + { + if (method_exists($user, 'getId')) { + return $user->getId(); + } + return $user->getUserIdentifier(); + } } diff --git a/Core/H5PSymfony.php b/Core/H5PSymfony.php index aaf1754..0bf88bf 100644 --- a/Core/H5PSymfony.php +++ b/Core/H5PSymfony.php @@ -4,24 +4,22 @@ namespace Studit\H5PBundle\Core; -use Doctrine\DBAL\DBALException; -use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\OptimisticLockException; -use Doctrine\ORM\ORMException; use Studit\H5PBundle\DependencyInjection\Configuration; use Studit\H5PBundle\Editor\EditorStorage; use Studit\H5PBundle\Entity\Content; use Studit\H5PBundle\Entity\ContentLibraries; +use Studit\H5PBundle\Entity\ContentRepository; use Studit\H5PBundle\Entity\Counters; use Studit\H5PBundle\Entity\LibrariesHubCache; use Studit\H5PBundle\Entity\LibrariesLanguages; use Studit\H5PBundle\Entity\Library; use Studit\H5PBundle\Entity\LibraryLibraries; +use Studit\H5PBundle\Entity\LibraryLibrariesRepository; +use Studit\H5PBundle\Entity\LibraryRepository; use Studit\H5PBundle\Event\H5PEvents; use Studit\H5PBundle\Event\LibrarySemanticsEvent; use GuzzleHttp\Client; -use H5PPermission; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; use Symfony\Component\HttpFoundation\RequestStack; @@ -133,7 +131,7 @@ public function getPlatformInfo() /** * Implements fetchExternalData * @param $url - * @param null $data + * @param null|mixed $data * @param bool $blocking * @param null $stream * @return bool|string @@ -347,8 +345,8 @@ public function loadLibraries() public function getAdminUrl() { // Misplaced; not used by Core. - $url = Url::fromUri('internal:/admin/content/h5p')->toString(); - return $url; + // $url = Url::fromUri('internal:/admin/content/h5p')->toString(); + return ''; } /** @@ -390,7 +388,9 @@ public function isPatchedLibrary($library) if ($this->getOption('dev_mode', FALSE)) { return TRUE; } - return $this->manager->getRepository('Studit\H5PBundle\Entity\Library')->isPatched($library); + /** @var LibraryRepository $repo */ + $repo = $this->manager->getRepository('Studit\H5PBundle\Entity\Library'); + return $repo->isPatched($library); } @@ -551,7 +551,7 @@ private function storeContent($contentData, Content $content) */ public function updateContent($contentData, $contentMainId = NULL) { - /** @var $content Content*/ + /** @var Content $content */ $content = $this->manager->getRepository('Studit\H5PBundle\Entity\Content')->find($contentData['id']); return $this->storeContent($contentData, $content); } @@ -677,9 +677,13 @@ public function getLibraryUsage($libraryId, $skipContent = FALSE) if ($skipContent) { $usage['content'] = -1; } else { - $usage['content'] = $this->manager->getRepository('Studit\H5PBundle\Entity\Library')->countContentLibrary($libraryId); + /** @var LibraryRepository $libraryRepository */ + $libraryRepository = $this->manager->getRepository('Studit\H5PBundle\Entity\Library'); + $usage['content'] = $libraryRepository->countContentLibrary($libraryId); } - $usage['libraries'] = $this->manager->getRepository('Studit\H5PBundle\Entity\LibraryLibraries')->countLibraries($libraryId); + /** @var LibraryLibrariesRepository $libraryLibrariesRepository */ + $libraryLibrariesRepository = $this->manager->getRepository('Studit\H5PBundle\Entity\LibraryLibraries'); + $usage['libraries'] = $libraryLibrariesRepository->countLibraries($libraryId); return $usage; } @@ -688,7 +692,9 @@ public function getLibraryUsage($libraryId, $skipContent = FALSE) */ public function loadLibrary($machineName, $majorVersion, $minorVersion) { - $library = $this->manager->getRepository('Studit\H5PBundle\Entity\Library')->findOneArrayBy(['machineName' => $machineName, 'majorVersion' => $majorVersion, 'minorVersion' => $minorVersion]); + /** @var LibraryRepository $libraryRepo */ + $libraryRepo = $this->manager->getRepository('Studit\H5PBundle\Entity\Library'); + $library = $libraryRepo->findOneArrayBy(['machineName' => $machineName, 'majorVersion' => $majorVersion, 'minorVersion' => $minorVersion]); if (!$library) { return false; } @@ -770,6 +776,7 @@ public function deleteLibrary($library) */ public function loadContent($id) { + return []; } /** @@ -852,7 +859,9 @@ public function clearFilteredParameters($library_id) */ public function getNumNotFiltered() { - return $this->manager->getRepository('Studit\H5PBundle\Entity\Content')->countNotFiltered(); + /** @var ContentRepository $contentRepo */ + $contentRepo = $this->manager->getRepository('Studit\H5PBundle\Entity\Content'); + return $contentRepo->countNotFiltered(); } /** @@ -860,7 +869,9 @@ public function getNumNotFiltered() */ public function getNumContent($libraryId, $skip = NULL) { - return $this->manager->getRepository('Studit\H5PBundle\Entity\Content')->countLibraryContent($libraryId); + /** @var ContentRepository $contentRepo */ + $contentRepo = $this->manager->getRepository('Studit\H5PBundle\Entity\Content'); + return $contentRepo->countLibraryContent($libraryId); } /** @@ -895,7 +906,9 @@ public function getLibraryStats($type) */ public function getNumAuthors() { - $contents = $this->manager->getRepository('Studit\H5PBundle\Entity\Content')->countContent(); + /** @var ContentRepository $contentRepo */ + $contentRepo = $this->manager->getRepository('Studit\H5PBundle\Entity\Content'); + $contents = $contentRepo->countContent(); // Return 1 if there is content and 0 if there is none return !$contents; } @@ -912,6 +925,7 @@ public function saveCachedAssets($key, $libraries) */ public function deleteCachedAssets($library_id) { + return []; } /** @@ -927,7 +941,9 @@ public function deleteCachedAssets($library_id) public function getLibraryContentCount() { $contentCount = []; - $results = $this->manager->getRepository('Studit\H5PBundle\Entity\Content')->libraryContentCount(); + /** @var ContentRepository $contentRepo */ + $contentRepo = $this->manager->getRepository('Studit\H5PBundle\Entity\Content'); + $results = $contentRepo->libraryContentCount(); // Format results foreach ($results as $library) { $contentCount[$library['machineName'] . " " . $library['majorVersion'] . "." . $library['minorVersion']] = $library[1]; @@ -1012,7 +1028,6 @@ public function replaceContentTypeCache($contentTypeCache) /** * @param $tableClassName - * @throws DBALException */ private function truncateTable($tableClassName) { @@ -1027,8 +1042,8 @@ private function truncateTable($tableClassName) /** * @inheritDoc */ - public function libraryHasUpgrade($library) + public function libraryHasUpgrade($library): bool { - // TODO: Implement libraryHasUpgrade() method. + return false; } } \ No newline at end of file diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f152ae5..fc0471f 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -2,6 +2,7 @@ namespace Studit\H5PBundle\DependencyInjection; +use RuntimeException; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\Definition\NodeInterface; @@ -19,12 +20,12 @@ public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder('studit_h5_p'); - if (method_exists($treeBuilder, 'getRootNode')) { - $rootNode = $treeBuilder->getRootNode(); - } else { - // BC layer for symfony/config 4.1 and older - $rootNode = $treeBuilder->root('studit_h5_p'); + $rootNode = $treeBuilder->getRootNode(); + + if (!method_exists($rootNode, 'children')) { + throw new RuntimeException('Your Symfony version does not support the children() method to define the root node in the H5P bundle configuration.'); } + $rootNode ->children() ->scalarNode('storage_dir')->defaultValue("h5p")->end() diff --git a/Editor/EditorAjax.php b/Editor/EditorAjax.php index 80f5f3c..daf302f 100644 --- a/Editor/EditorAjax.php +++ b/Editor/EditorAjax.php @@ -4,6 +4,8 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; +use Studit\H5PBundle\Entity\EventRepository; +use Studit\H5PBundle\Entity\LibraryRepository; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; class EditorAjax implements \H5PEditorAjaxInterface @@ -35,7 +37,9 @@ public function __construct(EntityManagerInterface $manager, TokenStorageInterfa */ public function getLatestLibraryVersions() { - return $this->manager->getRepository('Studit\H5PBundle\Entity\Library')->findLatestLibraryVersions(); + /** @var LibraryRepository $repo */ + $repo = $this->manager->getRepository('Studit\H5PBundle\Entity\Library'); + return $repo->findLatestLibraryVersions(); } @@ -69,7 +73,9 @@ public function getAuthorsRecentlyUsedLibraries() $recentlyUsed = []; $user = $this->tokenStorage->getToken()->getUser(); if (is_object($user)) { - $events = $this->manager->getRepository('Studit\H5PBundle\Entity\Event')->findRecentlyUsedLibraries($user->getId()); + /** @var EventRepository $repo */ + $repo = $this->manager->getRepository('Studit\H5PBundle\Entity\Event'); + $events = $repo->findRecentlyUsedLibraries(method_exists($user, 'getId') ? $user->getId() : (method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : -1)); foreach ($events as $event) { $recentlyUsed[] = $event['libraryName']; } @@ -96,5 +102,6 @@ public function validateEditorToken($token) */ public function getTranslations($libraries, $language_code) { + return []; } } \ No newline at end of file diff --git a/Editor/EditorStorage.php b/Editor/EditorStorage.php index bfa1cea..fcb9e66 100644 --- a/Editor/EditorStorage.php +++ b/Editor/EditorStorage.php @@ -5,10 +5,13 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Exception\NotSupported; use H5peditorFile; use Studit\H5PBundle\Core\H5POptions; use Studit\H5PBundle\Core\H5PSymfony; +use Studit\H5PBundle\Entity\LibrariesLanguagesRepository; use Studit\H5PBundle\Entity\Library; +use Studit\H5PBundle\Entity\LibraryRepository; use Studit\H5PBundle\Event\H5PEvents; use Studit\H5PBundle\Event\LibraryFileEvent; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -79,7 +82,9 @@ public function __construct(H5POptions $options, Filesystem $filesystem, Authori */ public function getLanguage($machineName, $majorVersion, $minorVersion, $language) { - return $this->entityManager->getRepository('Studit\H5PBundle\Entity\LibrariesLanguages')->findForLibrary($machineName, $majorVersion, $minorVersion, $language); + /** @var LibrariesLanguagesRepository $repo */ + $repo = $this->entityManager->getRepository('Studit\H5PBundle\Entity\LibrariesLanguages'); + return $repo->findForLibrary($machineName, $majorVersion, $minorVersion, $language); } /** @@ -92,12 +97,14 @@ public function getLanguage($machineName, $majorVersion, $minorVersion, $languag * @param string $machineName The machine readable name of the library(content type) * @param int $majorVersion Major part of version number * @param int $minorVersion Minor part of version number - * @param string $language Language code default is EN - * @return string Translation in JSON format + * @return string[] Translation in JSON format + * @throws NotSupported */ public function getAvailableLanguages($machineName, $majorVersion, $minorVersion) { - return $this->entityManager->getRepository('Studit\H5PBundle\Entity\LibrariesLanguages')->findForLibraryAllLanguages($machineName, $majorVersion, $minorVersion); + /** @var LibrariesLanguagesRepository $repo */ + $repo = $this->entityManager->getRepository('Studit\H5PBundle\Entity\LibrariesLanguages'); + return $repo->findForLibraryAllLanguages($machineName, $majorVersion, $minorVersion); } /** @@ -131,7 +138,9 @@ public function getLibraries($libraries = NULL) return $this->getLibrariesWithDetails($libraries, $canCreateRestricted); } $libraries = []; - $librariesResult = $this->entityManager->getRepository('Studit\H5PBundle\Entity\Library')->findAllRunnableWithSemantics(); + /** @var LibraryRepository $libraryRepo */ + $libraryRepo = $this->entityManager->getRepository('Studit\H5PBundle\Entity\Library'); + $librariesResult = $libraryRepo->findAllRunnableWithSemantics(); foreach ($librariesResult as $library) { //Decode metadata setting $library->metadataSettings = json_decode($library->metadataSettings); @@ -164,8 +173,10 @@ private function getLibrariesWithDetails($libraries, $canCreateRestricted) { $librariesWithDetails = []; foreach ($libraries as $library) { + /** @var LibraryRepository $repo */ + $repo = $this->entityManager->getRepository('Studit\H5PBundle\Entity\Library'); /** @var Library $details */ - $details = $this->entityManager->getRepository('Studit\H5PBundle\Entity\Library')->findHasSemantics($library->name, $library->majorVersion, $library->minorVersion); + $details = $repo->findHasSemantics($library->name, $library->majorVersion, $library->minorVersion); if ($details) { $library->tutorialUrl = $details->getTutorialUrl(); $library->title = $details->getTitle(); diff --git a/Editor/LibraryStorage.php b/Editor/LibraryStorage.php index 9a55a61..db00ce6 100644 --- a/Editor/LibraryStorage.php +++ b/Editor/LibraryStorage.php @@ -7,6 +7,7 @@ use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManagerInterface; use Studit\H5PBundle\Entity\Content; +use Studit\H5PBundle\Entity\LibraryRepository; class LibraryStorage { @@ -39,7 +40,9 @@ public function __construct(\H5PCore $core, \H5peditor $editor, EntityManagerInt public function storeLibraryData($library, $parameters, Content $content = null) { $libraryData = Utilities::getLibraryProperties($library); - $libraryData['libraryId'] = $this->entityManager->getRepository('Studit\H5PBundle\Entity\Library')->findIdBy($libraryData['machineName'], $libraryData['majorVersion'], $libraryData['minorVersion']); + /** @var LibraryRepository $libraryRepo */ + $libraryRepo = $this->entityManager->getRepository('Studit\H5PBundle\Entity\Library'); + $libraryData['libraryId'] = $libraryRepo->findIdBy($libraryData['machineName'], $libraryData['majorVersion'], $libraryData['minorVersion']); if ($content) { $oldLibrary = [ 'name' => $content->getLibrary()->getMachineName(), diff --git a/Entity/LibraryRepository.php b/Entity/LibraryRepository.php index 008430a..f00df46 100644 --- a/Entity/LibraryRepository.php +++ b/Entity/LibraryRepository.php @@ -4,6 +4,7 @@ namespace Studit\H5PBundle\Entity; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\DBAL\Statement; use Doctrine\ORM\AbstractQuery; use Doctrine\ORM\NoResultException; use Doctrine\Persistence\ManagerRegistry; @@ -75,9 +76,10 @@ public function findLatestLibraryVersions() hl4.has_icon EOT; $em = $this->getEntityManager(); + /** @var Statement $stmt */ $stmt = $em->getConnection()->prepare($sql); - $stmt->execute(); - $libraryVersions = $stmt->fetchAll(); + $result = $stmt->executeQuery(); + $libraryVersions = $result->fetchAllAssociative(); foreach ($libraryVersions as &$libraryVersion) { $libraryVersion = (object)$libraryVersion; } diff --git a/Event/H5PEvents.php b/Event/H5PEvents.php index 85a9d6e..f9df125 100644 --- a/Event/H5PEvents.php +++ b/Event/H5PEvents.php @@ -15,13 +15,13 @@ class H5PEvents extends \H5PEventBase const SEMANTICS = 'h5p.semantics'; /** - * @var $userid int + * @var int $userid */ private $userid; /** - * @var $em EntityManagerInterface - */ + * @var EntityManagerInterface $em + */ private $em; /** * H5PEvents constructor. @@ -34,7 +34,7 @@ class H5PEvents extends \H5PEventBase * @param int $userId * @param EntityManagerInterface $em */ - public function __construct($type, $sub_type = NULL, $content_id = NULL, $content_title = NULL, $library_name = NULL, $library_version = NULL, $userId= 0, EntityManagerInterface $em) + public function __construct(EntityManagerInterface $em, $type, $sub_type = NULL, $content_id = NULL, $content_title = NULL, $library_name = NULL, $library_version = NULL, $userId= 0) { parent::__construct($type, $sub_type, $content_id, $content_title, $library_name, $library_version); $this->userid = $userId; @@ -69,7 +69,7 @@ protected function save() protected function saveStats() { $type = $this->type . ' ' . $this->sub_type; - /** + /*/** * @var Counters $current_num */ /*$current_num = $this->em->getRepository("Studit\H5PBundle\Entity\Counters")->findOneBy(['type' => $type, 'libraryName' => $this->library_name, 'libraryVersion' => $this->library_version]); diff --git a/README.MD b/README.MD index 648fb1b..c44c44a 100644 --- a/README.MD +++ b/README.MD @@ -117,3 +117,11 @@ Changelog: - Fix bug with missing link img - Fix Download package - Store usage data and points + + +Developing: +------------- +Run the static analyzer like that: +```sh +php -d memory_limit=-1 vendor/bin/phpstan.phar analyze . +``` \ No newline at end of file diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 3daf396..b96a3ca 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -86,6 +86,7 @@ services: tags: - { name: 'console.command', command: 'h5p-bundle:IncludeAssetsCommand' } Studit\H5PBundle\Command\H5pBundleCleanUpFilesCommand: + autowire: true tags: - { name: 'console.command', command: 'h5p-bundle:cleanup-files'} diff --git a/composer.json b/composer.json index 9fb321c..5dcf50c 100644 --- a/composer.json +++ b/composer.json @@ -32,16 +32,20 @@ "guzzlehttp/guzzle": "^7.5", "h5p/h5p-core": "^1.24", "h5p/h5p-editor": "^1.25", - "symfony/framework-bundle": "~4.0|~5.0|~6.0|~7.0", - "symfony/serializer": "~4.0|~5.0|~6.0", + "symfony/framework-bundle": "~5.0|~6.0|~7.0", + "symfony/serializer": "~5.0|~6.0|~7.0", "twig/extra-bundle": "^3.0", "doctrine/doctrine-bundle": "^2.0", - "symfony/security-bundle": "~4.0|~5.0|~6.0|~7.0", - "symfony/asset": "~4.0|~5.0|~6.0|~7.0" + "symfony/security-bundle": "~5.0|~6.0|~7.0", + "symfony/asset": "~5.0|~6.0|~7.0", + "symfony/form": "~5.0|~6.0|~7.0" }, "autoload": { "psr-4": { "Studit\\H5PBundle\\": "" } + }, + "require-dev": { + "phpstan/phpstan": "^1.10" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..4a066c1 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,6 @@ +parameters: + excludePaths: + - vendor/* + level: 2 + paths: + - .