Collector::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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