FilterPipe::createFilter()   C
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

Changes 4
Bugs 1 Features 0
Metric Value
dl 0
loc 23
ccs 18
cts 18
cp 1
rs 5.8541
c 4
b 1
f 0
cc 9
eloc 18
nc 5
nop 1
crap 9
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
namespace Plum\Plum\Pipe;
12
13
use InvalidArgumentException;
14
use Plum\Plum\Filter\CallbackFilter;
15
use Plum\Plum\Filter\FilterInterface;
16
17
/**
18
 * FilterPipe.
19
 *
20
 * @author    Florian Eckerstorfer
21
 * @copyright 2014-2016 Florian Eckerstorfer
22
 */
23
class FilterPipe extends AbstractPipe
24
{
25
    /**
26
     * @param FilterInterface|callable|array $element
27
     *
28
     * @return FilterPipe
29
     */
30 7
    public static function createFilter($element)
31
    {
32 7
        if (is_callable($element)) {
33 1
            $filter = new CallbackFilter($element);
34 7
        } elseif (is_array($element) && isset($element['filter']) && is_callable($element['filter'])) {
35 1
            $filter = new CallbackFilter($element['filter']);
36 1
            unset($element['filter']);
37 6
        } elseif (is_array($element) && isset($element['filter']) && $element['filter'] instanceof FilterInterface) {
38 2
            $filter = $element['filter'];
39 2
            unset($element['filter']);
40 5
        } elseif ($element instanceof FilterInterface) {
41 1
            $filter = $element;
42 1
        } else {
43 2
            throw new InvalidArgumentException('Workflow::addFilter() must be called with either an instance of '.
44 2
                                               '"Plum\Plum\Filter\FilterInterface" or an array that contains '.
45 2
                                               '"filter".');
46
        }
47
48 5
        $pipe         = new self($element);
49 5
        $pipe->filter = $filter;
50
51 5
        return $pipe;
52
    }
53
}
54