Passed
Push — master ( f80879...93467d )
by Bjørn
03:47
created

ConverterFactory::makeConverterFromClassname()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.032

Importance

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