Refactor TempUrlOpenstackGenerator to use parameter bag and Bundle configuration

The TempUrlOpenstackGenerator now uses a ParameterBag for configuration instead of individual parameters. This simplifies the handling of configuration values and makes the code more maintainable. The parameter configuration has also been included in the chill_doc_store configuration array for a unified approach.
This commit is contained in:
Julien Fastré 2023-12-11 16:08:29 +01:00
parent d688022825
commit a70572266f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
4 changed files with 118 additions and 22 deletions

View File

@ -18,6 +18,7 @@ use Chill\DocStoreBundle\AsyncUpload\SignedUrlPost;
use Chill\DocStoreBundle\AsyncUpload\TempUrlGeneratorInterface; use Chill\DocStoreBundle\AsyncUpload\TempUrlGeneratorInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
@ -27,17 +28,28 @@ final readonly class TempUrlOpenstackGenerator implements TempUrlGeneratorInterf
{ {
private const CHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; private const CHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
private string $base_url;
private string $key;
private int $max_expire_delay;
private int $max_submit_delay;
private int $max_post_file_size;
private int $max_file_count;
public function __construct( public function __construct(
private LoggerInterface $logger, private LoggerInterface $logger,
private EventDispatcherInterface $event_dispatcher, private EventDispatcherInterface $event_dispatcher,
private ClockInterface $clock, private ClockInterface $clock,
private string $base_url, ParameterBagInterface $parameterBag,
private string $key, ) {
private int $max_expire_delay, $config = $parameterBag->get('chill_doc_store')['openstack']['temp_url'];
private int $max_submit_delay,
private int $max_post_file_size, $this->key = $config['temp_url_key'];
private int $max_file_count = 1 $this->base_url = $config['temp_url_base_path'];
) {} $this->max_expire_delay = $config['max_expires_delay'];
$this->max_submit_delay = $config['max_submit_delay'];
$this->max_post_file_size = $config['max_post_file_size'];
$this->max_file_count = $config['max_post_file_count'];
}
/** /**
* @throws TempUrlGeneratorException * @throws TempUrlGeneratorException
@ -115,7 +127,7 @@ final readonly class TempUrlOpenstackGenerator implements TempUrlGeneratorInterf
]; ];
$url = $url.'?'.\http_build_query($args); $url = $url.'?'.\http_build_query($args);
$signature = new SignedUrl($method, $url, $expires); $signature = new SignedUrl(strtoupper($method), $url, $expires);
$this->event_dispatcher->dispatch( $this->event_dispatcher->dispatch(
new TempUrlGenerateEvent($signature) new TempUrlGenerateEvent($signature)

View File

@ -32,6 +32,8 @@ class ChillDocStoreExtension extends Extension implements PrependExtensionInterf
$configuration = new Configuration(); $configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs); $config = $this->processConfiguration($configuration, $configs);
$container->setParameter('chill_doc_store', $config);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config')); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config'));
$loader->load('services.yaml'); $loader->load('services.yaml');
$loader->load('services/media.yaml'); $loader->load('services/media.yaml');
@ -94,7 +96,6 @@ class ChillDocStoreExtension extends Extension implements PrependExtensionInterf
'routing' => [ 'routing' => [
'resources' => [ 'resources' => [
'@ChillDocStoreBundle/config/routes.yaml', '@ChillDocStoreBundle/config/routes.yaml',
'@ChampsLibresAsyncUploaderBundle/config/routes.yaml',
], ],
], ],
]); ]);

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
namespace Chill\DocStoreBundle\DependencyInjection; namespace Chill\DocStoreBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\Definition\ConfigurationInterface;
@ -24,11 +25,68 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder() public function getConfigTreeBuilder()
{ {
$treeBuilder = new TreeBuilder('chill_doc_store'); $treeBuilder = new TreeBuilder('chill_doc_store');
/** @var ArrayNodeDefinition $rootNode */
$rootNode = $treeBuilder->getRootNode(); $rootNode = $treeBuilder->getRootNode();
// Here you should define the parameters that are allowed to /* @phpstan-ignore-next-line As there are inconsistencies in return types, but the code works... */
// configure your bundle. See the documentation linked above for $rootNode->children()
// more information on that topic. // openstack node
->arrayNode('openstack')
->info('parameters to authenticate and generate temp url against the openstack object storage service')
->addDefaultsIfNotSet()
->children()
// openstack.temp_url
->arrayNode('temp_url')
->addDefaultsIfNotSet()
->children()
// openstack.temp_url.temp_url_key
->scalarNode('temp_url_key')
->isRequired()->cannotBeEmpty()
->info('the temp url key')
->end()
->scalarNode('temp_url_base_path')
->isRequired()->cannotBeEmpty()
->info('the base path to add **before** the path to media. Must contains the container')
->end()
->scalarNode('container')
->info('the container name')
->isRequired()->cannotBeEmpty()
->end()
->integerNode('max_post_file_size')
->defaultValue(15_000_000)
->info('Maximum size of the posted file, in bytes')
->end()
->integerNode('max_post_file_count')
->defaultValue(1)
->info('Maximum number of files which may be posted at once using a POST operation using async upload')
->end()
->integerNode('max_expires_delay')
->defaultValue(180)
->info('the maximum of seconds a cryptographic signature '
.'will be valid for submitting a file. This should be '
.'short, to avoid uploading multiple files')
->end()
->integerNode('max_submit_delay')
->defaultValue(3600)
->info('the maximum of seconds between the upload of a file and '
.'a the submission of the form. This delay will also prevent '
.'the check of persistence of uploaded file. Should be long '
.'enough for keeping user-friendly forms')
->end()
->end() // end of children 's temp_url
->end() // end array temp_url
->end() // end of children's openstack
->end() // end of openstack
->end() // end of root's children
->end();
return $treeBuilder; return $treeBuilder;
} }

View File

@ -17,6 +17,7 @@ use Chill\DocStoreBundle\AsyncUpload\SignedUrlPost;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger; use Psr\Log\NullLogger;
use Symfony\Component\Clock\MockClock; use Symfony\Component\Clock\MockClock;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
/** /**
@ -34,16 +35,28 @@ class TempUrlOpenstackGeneratorTest extends TestCase
$logger = new NullLogger(); $logger = new NullLogger();
$eventDispatcher = new EventDispatcher(); $eventDispatcher = new EventDispatcher();
$clock = new MockClock($now); $clock = new MockClock($now);
$parameters = new ParameterBag(
[
'chill_doc_store' => [
'openstack' => [
'temp_url' => [
'temp_url_key' => $key,
'temp_url_base_path' => $baseUrl,
'max_post_file_size' => 150,
'max_post_file_count' => 1,
'max_expires_delay' => 1800,
'max_submit_delay' => 1800,
],
],
],
]
);
$generator = new TempUrlOpenstackGenerator( $generator = new TempUrlOpenstackGenerator(
$logger, $logger,
$eventDispatcher, $eventDispatcher,
$clock, $clock,
$baseUrl, $parameters,
$key,
1800,
1800,
150
); );
$signedUrl = $generator->generate($method, $objectName, $expireDelay); $signedUrl = $generator->generate($method, $objectName, $expireDelay);
@ -59,16 +72,28 @@ class TempUrlOpenstackGeneratorTest extends TestCase
$logger = new NullLogger(); $logger = new NullLogger();
$eventDispatcher = new EventDispatcher(); $eventDispatcher = new EventDispatcher();
$clock = new MockClock($now); $clock = new MockClock($now);
$parameters = new ParameterBag(
[
'chill_doc_store' => [
'openstack' => [
'temp_url' => [
'temp_url_key' => $key,
'temp_url_base_path' => $baseUrl,
'max_post_file_size' => 150,
'max_post_file_count' => 1,
'max_expires_delay' => 1800,
'max_submit_delay' => 1800,
],
],
],
]
);
$generator = new TempUrlOpenstackGenerator( $generator = new TempUrlOpenstackGenerator(
$logger, $logger,
$eventDispatcher, $eventDispatcher,
$clock, $clock,
$baseUrl, $parameters,
$key,
1800,
1800,
150
); );
$signedUrl = $generator->generatePost(); $signedUrl = $generator->generatePost();