FilterFactory::fromFilters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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