MetadataFinder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Finder;
15
16
use Graze\ArrayFilter\ArrayFilterInterface;
17
use Graze\DataFile\Node\FileNode;
18
use Graze\DataFile\Node\FileNodeCollectionInterface;
19
use Graze\DataStructure\Collection\CollectionInterface;
20
21
/**
22
 * Find files based on their metadata
23
 */
24
class MetadataFinder implements FileFinderInterface
25
{
26
    /**
27
     * @var ArrayFilterInterface
28
     */
29
    private $filter;
30
31
    /**
32
     * @param ArrayFilterInterface $filter
33
     */
34 4
    public function __construct(ArrayFilterInterface $filter)
35
    {
36 4
        $this->filter = $filter;
37 4
    }
38
39
    /**
40
     * @param FileNodeCollectionInterface $files
41
     *
42
     * @return CollectionInterface
43
     */
44
    public function findFiles(FileNodeCollectionInterface $files)
45
    {
46 3
        return $files->filter(function (FileNode $file) {
47 3
            $metadata = $file->getMetadata();
48 3
            if ($metadata) {
49 2
                return ($this->filter->matches($file->getMetadata()));
50
            } else {
51 1
                return false;
52
            }
53 3
        });
54
    }
55
}
56