Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
56.67% |
17 / 30 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ConverterFactory | |
56.67% |
17 / 30 |
|
33.33% |
1 / 3 |
23.72 | |
0.00% |
0 / 1 |
| converterIdToClassname | |
47.62% |
10 / 21 |
|
0.00% |
0 / 1 |
17.20 | |||
| makeConverterFromClassname | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 | |||
| makeConverter | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace WebPConvert\Convert; |
| 4 | |
| 5 | use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\ConverterNotFoundException; |
| 6 | use WebPConvert\Convert\Converters\AbstractConverter; |
| 7 | |
| 8 | /** |
| 9 | * Make converters from their ids. |
| 10 | * |
| 11 | * @package WebPConvert |
| 12 | * @author Bjørn Rosell <it@rosell.dk> |
| 13 | * @since Class available since Release 2.0.0 |
| 14 | */ |
| 15 | class ConverterFactory |
| 16 | { |
| 17 | /** |
| 18 | * Get classname of a converter (by id) |
| 19 | * |
| 20 | * @param string $converterId Id of converter (ie "cwebp") |
| 21 | * |
| 22 | * @throws ConverterNotFoundException If there is no converter with that id. |
| 23 | * @return string Fully qualified class name of converter |
| 24 | */ |
| 25 | public static function converterIdToClassname($converterId) |
| 26 | { |
| 27 | switch ($converterId) { |
| 28 | case 'ffmpeg': |
| 29 | $classNameShort = 'FFMpeg'; |
| 30 | break; |
| 31 | case 'imagickbinary': |
| 32 | $classNameShort = 'ImagickBinary'; |
| 33 | break; |
| 34 | case 'imagemagick': |
| 35 | $classNameShort = 'ImageMagick'; |
| 36 | break; |
| 37 | case 'gmagickbinary': |
| 38 | $classNameShort = 'GmagickBinary'; |
| 39 | break; |
| 40 | case 'graphicsmagick': |
| 41 | $classNameShort = 'GraphicsMagick'; |
| 42 | break; |
| 43 | default: |
| 44 | $classNameShort = ucfirst($converterId); |
| 45 | } |
| 46 | $className = 'WebPConvert\\Convert\\Converters\\' . $classNameShort; |
| 47 | if (is_callable([$className, 'convert'])) { |
| 48 | return $className; |
| 49 | } else { |
| 50 | throw new ConverterNotFoundException('There is no converter with id:' . $converterId); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Make a converter instance by class name. |
| 56 | * |
| 57 | * @param string $converterClassName Fully qualified class name |
| 58 | * @param string $source The path to the file to convert |
| 59 | * @param string $destination The path to save the converted file to |
| 60 | * @param array $options (optional) |
| 61 | * @param \WebPConvert\Loggers\BaseLogger $logger (optional) |
| 62 | * |
| 63 | * @throws ConverterNotFoundException If the specified converter class isn't found |
| 64 | * @return AbstractConverter An instance of the specified converter |
| 65 | */ |
| 66 | public static function makeConverterFromClassname( |
| 67 | $converterClassName, |
| 68 | $source, |
| 69 | $destination, |
| 70 | $options = [], |
| 71 | $logger = null |
| 72 | ) { |
| 73 | if (!is_callable([$converterClassName, 'convert'])) { |
| 74 | throw new ConverterNotFoundException( |
| 75 | 'There is no converter with class name:' . $converterClassName . ' (or it is not a converter)' |
| 76 | ); |
| 77 | } |
| 78 | //$converter = new $converterClassName($source, $destination, $options, $logger); |
| 79 | |
| 80 | return call_user_func( |
| 81 | [$converterClassName, 'createInstance'], |
| 82 | $source, |
| 83 | $destination, |
| 84 | $options, |
| 85 | $logger |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Make a converter instance by either id or class name. |
| 91 | * |
| 92 | * @param string $converterIdOrClassName Either a converter ID or a fully qualified class name |
| 93 | * @param string $source The path to the file to convert |
| 94 | * @param string $destination The path to save the converted file to |
| 95 | * @param array $options (optional) |
| 96 | * @param \WebPConvert\Loggers\BaseLogger $logger (optional) |
| 97 | * |
| 98 | * @throws ConverterNotFoundException If the specified converter class isn't found |
| 99 | * @return AbstractConverter An instance of the specified converter |
| 100 | */ |
| 101 | public static function makeConverter($converterIdOrClassName, $source, $destination, $options = [], $logger = null) |
| 102 | { |
| 103 | // We take it that all lowercase means it is an id rather than a class name |
| 104 | if (strtolower($converterIdOrClassName) == $converterIdOrClassName) { |
| 105 | $converterClassName = self::converterIdToClassname($converterIdOrClassName); |
| 106 | } else { |
| 107 | $converterClassName = $converterIdOrClassName; |
| 108 | } |
| 109 | |
| 110 | return self::makeConverterFromClassname($converterClassName, $source, $destination, $options, $logger); |
| 111 | } |
| 112 | } |