GetWithMetadata::getMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace League\Flysystem\Plugin;
4
5
use InvalidArgumentException;
6
7
class GetWithMetadata extends AbstractPlugin
8
{
9
    /**
10
     * Get the method name.
11
     *
12
     * @return string
13
     */
14 3
    public function getMethod()
15
    {
16 3
        return 'getWithMetadata';
17
    }
18
19
    /**
20
     * Get metadata for an object with required metadata.
21
     *
22
     * @param string $path     path to file
23
     * @param array  $metadata metadata keys
24
     *
25
     * @throws InvalidArgumentException
26
     *
27
     * @return array metadata
28
     */
29 9
    public function handle($path, array $metadata)
30
    {
31 9
        $object = $this->filesystem->getMetadata($path);
32
33 9
        if ( ! $object) {
34 3
            return false;
35
        }
36
37 6
        $keys = array_diff($metadata, array_keys($object));
38
39 6
        foreach ($keys as $key) {
40 6
            if ( ! method_exists($this->filesystem, $method = 'get' . ucfirst($key))) {
41 3
                throw new InvalidArgumentException('Could not fetch metadata: ' . $key);
42
            }
43
44 3
            $object[$key] = $this->filesystem->{$method}($path);
45 3
        }
46
47 3
        return $object;
48
    }
49
}
50