Collector   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 9 3
A extract() 0 11 1
A __construct() 0 4 2
A add() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Media\Domain\Metadata;
6
7
use Damax\Common\Domain\Model\Metadata;
8
9
final class Collector implements Reader
10
{
11
    /**
12
     * @var Reader[]
13
     */
14
    private $items = [];
15
16
    public function __construct(iterable $items = [])
17
    {
18
        foreach ($items as $item) {
19
            $this->add($item);
20
        }
21
    }
22
23
    public function add(Reader $reader): void
24
    {
25
        $this->items[] = $reader;
26
    }
27
28
    public function supports($context): bool
29
    {
30
        foreach ($this->items as $item) {
31
            if ($item->supports($context)) {
32
                return true;
33
            }
34
        }
35
36
        return false;
37
    }
38
39
    public function extract($context): Metadata
40
    {
41
        $reduce = function (Metadata $metadata, Reader $reader) use ($context): Metadata {
42
            return $metadata->merge($reader->extract($context));
43
        };
44
45
        $filter = function (Reader $reader) use ($context): bool {
46
            return $reader->supports($context);
47
        };
48
49
        return array_reduce(array_filter($this->items, $filter), $reduce, Metadata::create());
50
    }
51
}
52