Completed
Push — master ( 80b762...b17462 )
by Bjørn
05:19
created

ConverterFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 90
ccs 21
cts 30
cp 0.7
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A converterIdToClassname() 0 20 5
A makeConverterFromClassname() 0 20 2
A makeConverter() 0 10 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 <[email protected]>
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 1
    public static function converterIdToClassname($converterId)
26
    {
27
        switch ($converterId) {
28 1
            case 'imagickbinary':
29
                $classNameShort = 'ImagickBinary';
30
                break;
31 1
            case 'imagemagick':
32
                $classNameShort = 'ImageMagick';
33
                break;
34 1
            case 'gmagickbinary':
35
                $classNameShort = 'GmagickBinary';
36
                break;
37
            default:
38 1
                $classNameShort = ucfirst($converterId);
39
        }
40 1
        $className = 'WebPConvert\\Convert\\Converters\\' . $classNameShort;
41 1
        if (is_callable([$className, 'convert'])) {
42
            return $className;
43
        } else {
44 1
            throw new ConverterNotFoundException('There is no converter with id:' . $converterId);
45
        }
46
    }
47
48
    /**
49
     * Make a converter instance by class name.
50
     *
51
     * @param  string  $converterClassName    Fully qualified class name
52
     * @param  string  $source                The path to the file to convert
53
     * @param  string  $destination           The path to save the converted file to
54
     * @param  array   $options               (optional)
55
     * @param  \WebPConvert\Loggers\BaseLogger   $logger       (optional)
56
     *
57
     * @throws ConverterNotFoundException  If the specified converter class isn't found
58
     * @return AbstractConverter  An instance of the specified converter
59
     */
60 1
    public static function makeConverterFromClassname(
61
        $converterClassName,
62
        $source,
63
        $destination,
64
        $options = [],
65
        $logger = null
66
    ) {
67 1
        if (!is_callable([$converterClassName, 'convert'])) {
68
            throw new ConverterNotFoundException(
69
                'There is no converter with class name:' . $converterClassName . ' (or it is not a converter)'
70
            );
71
        }
72
        //$converter = new $converterClassName($source, $destination, $options, $logger);
73
74 1
        return call_user_func(
75 1
            [$converterClassName, 'createInstance'],
76 1
            $source,
77 1
            $destination,
78 1
            $options,
79 1
            $logger
80
        );
81
    }
82
83
    /**
84
     * Make a converter instance by either id or class name.
85
     *
86
     * @param  string  $converterIdOrClassName   Either a converter ID or a fully qualified class name
87
     * @param  string  $source                   The path to the file to convert
88
     * @param  string  $destination              The path to save the converted file to
89
     * @param  array   $options                  (optional)
90
     * @param  \WebPConvert\Loggers\BaseLogger   $logger       (optional)
91
     *
92
     * @throws ConverterNotFoundException  If the specified converter class isn't found
93
     * @return AbstractConverter  An instance of the specified converter
94
     */
95 2
    public static function makeConverter($converterIdOrClassName, $source, $destination, $options = [], $logger = null)
96
    {
97
        // We take it that all lowercase means it is an id rather than a class name
98 2
        if (strtolower($converterIdOrClassName) == $converterIdOrClassName) {
99 1
            $converterClassName = self::converterIdToClassname($converterIdOrClassName);
100
        } else {
101 1
            $converterClassName = $converterIdOrClassName;
102
        }
103
104 1
        return self::makeConverterFromClassname($converterClassName, $source, $destination, $options, $logger);
105
    }
106
}
107