Completed
Push — master ( 4e7b0a...f8136b )
by Florian
02:06
created

ConverterPipe::createConverter()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 5
Bugs 1 Features 0
Metric Value
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 8.7624
c 5
b 1
f 0
cc 5
eloc 16
nc 5
nop 1
crap 5
1
<?php
2
3
/**
4
 * This file is part of plumphp/plum.
5
 *
6
 * (c) Florian Eckerstorfer <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Plum\Plum\Pipe;
13
14
use InvalidArgumentException;
15
use Plum\Plum\Converter\CallbackConverter;
16
use Plum\Plum\Converter\ConverterInterface;
17
18
/**
19
 * ConverterPipe.
20
 *
21
 * @author    Florian Eckerstorfer
22
 * @copyright 2014-2016 Florian Eckerstorfer
23
 */
24
class ConverterPipe extends AbstractPipe
25
{
26
    /**
27
     * @param ConverterInterface|callable|array $element
28
     *
29
     * @return ConverterPipe
30
     */
31 11
    public static function createConverter($element)
32
    {
33 11
        if (is_callable($element)) {
34 1
            $converter = new CallbackConverter($element);
35 11
        } elseif (self::hasElementCallbackConverter($element)) {
36 1
            $converter = new CallbackConverter($element['converter']);
37 10
        } elseif ($element instanceof ConverterInterface) {
38 1
            $converter = $element;
39 9
        } elseif (self::hasElementConverter($element)) {
40 6
            $converter = $element['converter'];
41 6
        } else {
42 2
            throw new InvalidArgumentException('Workflow::addConverter() must be called with either an instance of '.
43 2
                                               '"Plum\Plum\Converter\ConverterInterface" or with an array that '.
44 2
                                               'contains "converter".');
45
        }
46
47 9
        $pipe            = new self($element);
48 9
        $pipe->converter = $converter;
49
50 9
        return $pipe;
51
    }
52
53
    /**
54
     * @param array|callable|ConverterInterface $element
55
     *
56
     * @return bool
57
     */
58 1
    protected static function hasElementCallbackConverter($element)
59
    {
60 1
        return is_array($element) && isset($element['converter']) && is_callable($element['converter']);
61
    }
62
63
    /**
64
     * @param array|callable|ConverterInterface $element
65
     *
66
     * @return bool
67
     */
68 1
    protected static function hasElementConverter($element)
69
    {
70 1
        return is_array($element)
71 1
               && isset($element['converter'])
72 1
               && $element['converter'] instanceof ConverterInterface;
73
    }
74
}
75