Skip to content

Commit

Permalink
Merge pull request #57 from jorisdugue/v3
Browse files Browse the repository at this point in the history
Optimize and implement missing documentation
  • Loading branch information
jorisdugue authored Nov 10, 2024
2 parents 78aa9f1 + eca4ad0 commit 52ec80d
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 47 deletions.
20 changes: 13 additions & 7 deletions Core/H5PIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,37 @@ class H5PIntegration extends H5PUtils
/**
* @var EntityManager
*/
private $entityManager;
private EntityManager $entityManager;

/**
* @var \H5PCore
*/
private $core;
private \H5PCore $core;

/**
* @var RouterInterface
*/
private $router;
private RouterInterface $router;

/**
* @var H5POptions
*/
private $options;
private H5POptions $options;

/**
* @var RequestStack
*/
private $requestStack;
private RequestStack $requestStack;

/**
* @var Packages
*/
private $assetsPaths;
private Packages $assetsPaths;

/**
* @var \H5PContentValidator
*/
private $contentValidator;
private \H5PContentValidator $contentValidator;

/**
* H5PContent constructor.
Expand Down
79 changes: 57 additions & 22 deletions Core/H5POptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ class H5POptions
/**
* @var array
*/
private $config;
private array|null $config;

/**
* @var array
*/
private $storedConfig = null;
private array|null $storedConfig = null;

private $h5pPath;
private $folderPath;
private $projectRootDir;

/**
* @var EntityManagerInterface
*/
private $manager;
private EntityManagerInterface $manager;

/**
* H5POptions constructor.
Expand All @@ -40,15 +42,23 @@ public function __construct(?array $config, $projectRootDir, EntityManagerInterf
}

/**
* @param $name
* @param $default
* @return mixed|null
* Retrieves the value of a configuration option.
*
* This method fetches the specified configuration option's value. If the option is found in the cached
* `storedConfig`, it returns that value. If not, it checks the local `config` array. If the option is
* not found in either, it returns the provided default value.
*
* @param string $name The name of the configuration option.
* @param mixed $default The default value to return if the option is not found in either `storedConfig` or `config`.
*
* @return mixed|null The value of the configuration option, or the default value if the option is not set.
*/
public function getOption($name, $default = null)
{
try {
$this->retrieveStoredConfig();
} catch (DriverException $e) {
} catch (DriverException) {
// Suppress database errors and continue
}

if (isset($this->storedConfig[$name])) {
Expand All @@ -61,9 +71,20 @@ public function getOption($name, $default = null)
}

/**
* @throws InvalidArgumentException
* Sets or updates a configuration option in the database.
*
* This method updates the value of a specified configuration option. If the option already exists
* and its current value differs from the provided value, the method updates it. If the option does
* not exist, it creates a new one. Changes are persisted to the database.
*
* @param string $name The name of the configuration option.
* @param string|int|null $value The value to set for the configuration option.
*
* @throws InvalidArgumentException If the provided option name or value is invalid.
*
* @return void
*/
public function setOption($name, $value): void
public function setOption(string $name, string|int|null $value): void
{
$this->retrieveStoredConfig();

Expand All @@ -80,15 +101,23 @@ public function setOption($name, $value): void
}

/**
* Retrieves and caches configuration options from the database.
*
* This method loads all configuration options from the database if they haven't been loaded yet.
* The options are stored as key-value pairs in the `$storedConfig` property for easy access.
* If `storedConfig` is already populated, the method does nothing to avoid redundant database queries.
*
* @return void
*/
private function retrieveStoredConfig(): void
{
if ($this->storedConfig === null) {
$this->storedConfig = [];
$options = $this->manager->getRepository('Studit\H5PBundle\Entity\Option')->findAll();
foreach ($options as $option) {
$this->storedConfig[$option->getName()] = $option->getValue();
if (!empty($options)) {
foreach ($options as $option) {
$this->storedConfig[$option->getName()] = $option->getValue();
}
}
}
}
Expand Down Expand Up @@ -120,27 +149,33 @@ public function getUploadedH5pPath($set = null)
}

/**
* @return mixed|string|null
* Helper function to ensure storage_dir always starts with a '/'.
*
* @return string
*/
public function getRelativeH5PPath()
private function formatStorageDir(): string
{
$dir = $this->getOption('storage_dir');
// Setup the default value to empty string
$dir = $this->getOption('storage_dir', [""]);
return $dir[0] === '/' ? $dir : "/{$dir}";
}

public function getAbsoluteH5PPathWithSlash(): string
/**
* @return string
*/
public function getRelativeH5PPath(): string
{
$dir = $this->getOption('storage_dir');
$dir = $dir[0] === '/' ? $dir : "/{$dir}";
return $this->formatStorageDir();
}

return $this->getAbsoluteWebPath() . $dir . '/';
public function getAbsoluteH5PPathWithSlash(): string
{
return $this->getAbsoluteWebPath() . $this->formatStorageDir() . '/';
}

public function getAbsoluteH5PPath(): string
{
$dir = $this->getOption('storage_dir');
$dir = $dir[0] === '/' ? $dir : "/{$dir}";

return $this->getAbsoluteWebPath() . $dir;
return rtrim($this->getAbsoluteWebPath(), '/') . $this->formatStorageDir();
}

public function getAbsoluteWebPath(): string
Expand Down
3 changes: 2 additions & 1 deletion Event/H5PEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class H5PEvents extends \H5PEventBase
/**
* @var EntityManagerInterface $em
*/
private $em;
private EntityManagerInterface $em;

/**
* H5PEvents constructor.
* @param $type
Expand Down
39 changes: 22 additions & 17 deletions Event/LibrarySemanticsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,54 @@

class LibrarySemanticsEvent extends Event
{
private $semantics;
private $name;
private $majorVersion;
private $minorVersion;
private array $semantics;
private string $name;
private int $majorVersion;
private int $minorVersion;

/**
* LibrarySemanticsEvent constructor.
* @param $semantics
* @param $name
* @param $majorVersion
* @param $minorVersion
* @param array $semantics array of semantics
* @param string $name Nom of package
* @param int $majorVersion number of major version
* @param int $minorVersion number of minor version
*/
public function __construct($semantics, $name, $majorVersion, $minorVersion)
public function __construct(array $semantics, string $name, int $majorVersion, int $minorVersion)
{
$this->semantics = $semantics;
$this->name = $name;
$this->majorVersion = $majorVersion;
$this->minorVersion = $minorVersion;
}

/**
* @return mixed
* @return array
*/
public function getSemantics()
public function getSemantics(): array
{
return $this->semantics;
}

/**
* @return mixed
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}

/**
* @return mixed
* @return int
*/
public function getMajorVersion()
public function getMajorVersion(): int
{
return $this->majorVersion;
}

/**
* @return mixed
* @return int
*/
public function getMinorVersion()
public function getMinorVersion(): int
{
return $this->minorVersion;
}
Expand Down
116 changes: 116 additions & 0 deletions Tests/Core/H5PIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Studit\H5PBundle\Tests\Core;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use H5PCore;
use H5peditor;
use H5PContentValidator;
use H5PFrameworkInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Studit\H5PBundle\Core\H5PIntegration;
use Studit\H5PBundle\Core\H5POptions;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

class H5PIntegrationTest extends TestCase
{
private H5PIntegration|MockObject $h5pIntegration;
private H5POptions|MockObject $options;
private H5PCore|MockObject $core;
private EntityManagerInterface|MockObject $entityManager;
private RouterInterface|MockObject $router;
private RequestStack|MockObject $requestStack;
private Packages|MockObject $assetsPaths;
private H5PContentValidator|MockObject $contentValidator;

protected function setUp(): void
{
$this->core = $this->createMock(H5PCore::class);
$this->options = $this->createMock(H5POptions::class);
$tokenStorage = $this->createMock(TokenStorageInterface::class);
$this->entityManager = $this->createMock(EntityManager::class);
$this->router = $this->createMock(RouterInterface::class);
$this->requestStack = $this->createMock(RequestStack::class);
$this->assetsPaths = $this->createMock(Packages::class);
$this->contentValidator = $this->createMock(H5PContentValidator::class);

// Création de l'instance de H5PIntegration pour les tests
$this->h5pIntegration = new H5PIntegration(
$this->core,
$this->options,
$tokenStorage,
$this->entityManager,
$this->router,
$this->requestStack,
$this->assetsPaths,
$this->contentValidator
);
}

public function testGetGenericH5PIntegrationSettings()
{
$request = new Request();
$this->requestStack->method('getMainRequest')->willReturn($request);

$this->options->method('getOption')->willReturnMap([
['save_content_state', false, true],
['save_content_frequency', 30, 30],
['hub_is_enabled', true, true]
]);
$h5pFrameworkMock = $this->createMock(H5PFrameworkInterface::class);
$h5pFrameworkMock->method('getLibraryConfig')->willReturn(['someKey' => 'someValue']);

// Injectez le mock H5PFramework dans H5PCore
$this->core->h5pF = $h5pFrameworkMock;

$settings = $this->h5pIntegration->getGenericH5PIntegrationSettings();

$this->assertIsArray($settings);
$this->assertArrayHasKey('baseUrl', $settings);
$this->assertArrayHasKey('ajax', $settings);
$this->assertArrayHasKey('l10n', $settings);
}

public function testGetCoreAssets()
{
$this->options->method('getH5PAssetPath')->willReturn('/assets/h5p');
H5PCore::$scripts = ['script1.js', 'script2.js'];
H5PCore::$styles = ['style1.css', 'style2.css'];

$assets = $this->h5pIntegration->getCoreAssets();

$this->assertIsArray($assets);
$this->assertArrayHasKey('scripts', $assets);
$this->assertArrayHasKey('styles', $assets);
$this->assertCount(2, $assets['scripts']);
$this->assertCount(2, $assets['styles']);
}

public function testGetCacheBuster()
{
H5PCore::$coreApi = ['majorVersion' => 1, 'minorVersion' => 2];
$cacheBuster = $this->h5pIntegration->getCacheBuster();

$this->assertEquals('?=1.2', $cacheBuster);
}

public function testGetTranslationFilePath()
{
$request = new Request();
$request->setLocale('en');
$this->requestStack->method('getCurrentRequest')->willReturn($request);

$this->options->method('getAbsoluteWebPath')->willReturn('/web');

$translationFilePath = $this->h5pIntegration->getTranslationFilePath();

$this->assertStringContainsString('/h5p-editor/language/en.js', $translationFilePath);
}

}
Loading

0 comments on commit 52ec80d

Please sign in to comment.