Skip to content

Commit

Permalink
resolve options one by one ; factorize common code
Browse files Browse the repository at this point in the history
  • Loading branch information
jygaulier committed Oct 30, 2024
1 parent 74d10b3 commit 89f555c
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 287 deletions.
18 changes: 2 additions & 16 deletions lib/php/rendition-factory/src/Config/ModuleOptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,8 @@ public function __construct(private TemplateResolverInterface $templateResolver)
{
}

public function resolveOptions(array $options, array $context): array
public function resolveOption(mixed $option, array $context): mixed
{
return $this->compile($options, $context);
}

private function compile(array $options, array $context): array
{
$r = [];
foreach ($options as $k => $o) {
if (is_array($o)) {
$r[$k] = $this->compile($o, $context);
} else {
$r[$k] = is_string($o) ? $this->templateResolver->resolve($o, $context) : $o;
}
}

return $r;
return is_string($option) ? $this->templateResolver->resolve($option, $context) : $option;
}
}
39 changes: 29 additions & 10 deletions lib/php/rendition-factory/src/Transformer/Video/FFMpegHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,46 @@

namespace Alchemy\RenditionFactory\Transformer\Video;

use Alchemy\RenditionFactory\Context\TransformationContextInterface;
use FFMpeg\FFMpeg;
use FFMpeg;

class FFMpegHelper
{
public static function createFFMpeg(array $options, TransformationContextInterface $context): FFMpeg
public static function createFFMpeg(array $options): FFMpeg\FFMpeg
{
$ffmpegOptions = [];
if ($timeout = $options['timeout'] ?? 3600) {
if (!is_int($timeout)) {
throw new \InvalidArgumentException('Invalid timeout');
}
$ffmpegOptions['timeout'] = $timeout;

if (!is_int($timeout = $options['timeout'] ?? 3600) || $timeout < 1) {
throw new \InvalidArgumentException('Invalid timeout');
}
if ($threads = $options['threads'] ?? null) {
$ffmpegOptions['timeout'] = $timeout;

if ($threads = $options['threads']) {
if (!is_int($threads) || $threads < 1) {
throw new \InvalidArgumentException('Invalid threads count');
}
$ffmpegOptions['ffmpeg.threads'] = $threads;
}

return FFMpeg::create($ffmpegOptions, $context->getLogger());
return FFMpeg\FFMpeg::create($ffmpegOptions, $options['logger'] ?? null);
}

public static function pointAsText(FFMpeg\Coordinate\Point $point): string
{
return sprintf('(%d, %d)', $point->getX(), $point->getY());
}

public static function dimensionAsText(FFMpeg\Coordinate\Dimension $dimension): string
{
return sprintf('%d x %d', $dimension->getWidth(), $dimension->getHeight());
}

public static function coordAsText(array $coord): string
{
$s = [];
foreach ($coord as $k => $v) {
$s[] = sprintf('%s=%d', $k, $v);
}

return '['.implode(', ', $s).']';
}
}
Loading

0 comments on commit 89f555c

Please sign in to comment.