Completed
Push — master ( 97ba20...eac1d0 )
by Valentin
02:31
created

Metadata   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 74
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B fetch() 0 30 6
A getKeyValues() 0 4 1
1
<?php
2
3
namespace ExiftoolReader;
4
5
use ExiftoolReader\Config\Mapper;
6
7
/**
8
 * Class Metadata
9
 */
10
class Metadata
11
{
12
    /**
13
     * @var Mapper
14
     */
15
    private $config;
16
17
    /**
18
     * @var Utils
19
     */
20
    private $utils;
21
22
    /**
23
     * Metadata constructor.
24
     *
25
     * @param Mapper $config
26
     * @param Utils  $utils
27
     */
28
    public function __construct(Mapper $config, Utils $utils)
29
    {
30
        $this->config = $config;
31
        $this->utils = $utils;
32
    }
33
34
    /**
35
     * Fetch metadata by provided keys.
36
     *
37
     * @param Result        $input
38
     * @param array         $keys
39
     * @param callable|null $keyCallback
40
     * @param callable|null $valueCallback
41
     * @return array
42
     */
43
    public function fetch(
44
        Result $input,
45
        array $keys,
46
        callable $keyCallback = null,
47
        callable $valueCallback = null
48
    ) {
49
        $output = [];
50
        $decoded = $input->getDecoded();
51
52
        foreach ($keys as $key) {
53
            foreach ($this->getKeyValues($decoded, $key) as $name => $value) {
54
                //Get first not-empty value.
55
                if (!empty($value)) {
56
                    if (!empty($keyCallback)) {
57
                        $key = call_user_func($keyCallback, $key);
58
                    }
59
60
                    if (!empty($valueCallback)) {
61
                        $value = call_user_func($valueCallback, $value);
62
                    }
63
64
                    $output[$key] = $value;
65
66
                    continue;
67
                }
68
            }
69
        }
70
71
        return $output;
72
    }
73
74
    /**
75
     * @param array  $data
76
     * @param string $key
77
     * @return array
78
     */
79
    protected function getKeyValues(array $data, $key)
80
    {
81
        return $this->utils->fetchKeys($data, $this->config->getAliases($key));
82
    }
83
}