GetWithMetadata::handle()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 10
nc 4
nop 2
crap 4
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