ExtensionFilter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 35
ccs 11
cts 11
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 13 3
A __construct() 0 3 1
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\Message\UploadedFile\Filter;
15
16
use CoiSA\Http\Message\UploadedFile\FilterInterface;
17
use Psr\Http\Message\UploadedFileInterface;
18
19
/**
20
 * Class ExtensionFilter
21
 *
22
 * @package CoiSA\Http\Message\UploadedFile
23
 */
24
final class ExtensionFilter implements FilterInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    private $extension;
30
31
    /**
32
     * ExtensionFilter constructor.
33
     *
34
     * @param string $extension
35
     */
36 4
    public function __construct(string $extension)
37
    {
38 4
        $this->extension = \ltrim($extension, '.');
39 4
    }
40
41
    /**
42
     * @param UploadedFileInterface ...$uploadedFiles
43
     *
44
     * @return \Iterator
45
     */
46 3
    public function filter(UploadedFileInterface ...$uploadedFiles): \Iterator
47
    {
48 3
        foreach ($uploadedFiles as $uploadedFile) {
49 3
            $extension = \pathinfo(
50 3
                $uploadedFile->getClientFilename(),
51 3
                PATHINFO_EXTENSION
52
            );
53
54 3
            if ($extension !== $this->extension) {
55 2
                continue;
56
            }
57
58 2
            yield $uploadedFile;
59
        }
60 3
    }
61
}
62