FilterFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 41
ccs 0
cts 17
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromFilters() 0 3 1
A __invoke() 0 16 4
1
<?php
2
3
/**
4
 * This file is part of coisa/http.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\Http\Client;
15
16
use CoiSA\Http\Message\UploadedFile\Filter\ExtensionFilter;
17
use CoiSA\Http\Message\UploadedFile\Filter\FilterAggregator;
18
use CoiSA\Http\Message\UploadedFile\Filter\MediaTypeFilter;
19
use CoiSA\Http\Message\UploadedFile\FilterInterface;
20
use Psr\Container\ContainerInterface;
21
22
/**
23
 * Class FilterFactory
24
 *
25
 * @package CoiSA\Http\Client
26
 */
27
final class FilterFactory
28
{
29
    /**
30
     * @const array
31
     */
32
    const DEFAULT_FILTERS = [
33
        ExtensionFilter::class,
34
        MediaTypeFilter::class
35
    ];
36
37
    /**
38
     * @param ContainerInterface $container
39
     *
40
     * @return FilterInterface
41
     */
42
    public function __invoke(ContainerInterface $container): FilterInterface
43
    {
44
        if ($container->has(FilterAggregator::class)) {
45
            return $container->get(FilterAggregator::class);
46
        }
47
48
        $filters = [];
49
        foreach (self::DEFAULT_FILTERS as $filter) {
50
            if (!$container->has($filter)) {
51
                continue;
52
            }
53
54
            $filters[] = $container->get($filter);
55
        }
56
57
        return $this->fromFilters(...$filters);
58
    }
59
60
    /**
61
     * @param FilterInterface ...$filters
62
     *
63
     * @return FilterAggregator
64
     */
65
    public function fromFilters(FilterInterface ...$filters): FilterAggregator
66
    {
67
        return new FilterAggregator(...$filters);
68
    }
69
}
70