ListWith::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 3
crap 3
1
<?php
2
3
namespace League\Flysystem\Plugin;
4
5
class ListWith extends AbstractPlugin
6
{
7
    /**
8
     * Get the method name.
9
     *
10
     * @return string
11
     */
12 3
    public function getMethod()
13
    {
14 3
        return 'listWith';
15
    }
16
17
    /**
18
     * List contents with metadata.
19
     *
20
     * @param array  $keys
21
     * @param string $directory
22
     * @param bool   $recursive
23
     *
24
     * @return array listing with metadata
25
     */
26 6
    public function handle(array $keys = array(), $directory = '', $recursive = false)
27
    {
28 6
        $contents = $this->filesystem->listContents($directory, $recursive);
29
30 6
        foreach ($contents as $index => $object) {
31 6
            if ($object['type'] === 'file') {
32 6
                $missingKeys = array_diff($keys, array_keys($object));
33 6
                $contents[$index] = array_reduce($missingKeys, array($this, 'getMetadataByName'), $object);
34 3
            }
35 3
        }
36
37 3
        return $contents;
38
    }
39
40
    /**
41
     * Get a meta-data value by key name.
42
     *
43
     * @param array $object
44
     * @param       $key
45
     *
46
     * @return array
47
     */
48 6
    protected function getMetadataByName(array $object, $key)
49
    {
50 6
        $method = 'get' . ucfirst($key);
51
52 6
        if ( ! method_exists($this->filesystem, $method)) {
53 3
            throw new \InvalidArgumentException('Could not get meta-data for key: ' . $key);
54
        }
55
56 3
        $object[$key] = $this->filesystem->{$method}($object['path']);
57
58 3
        return $object;
59
    }
60
}
61