ContentListingFormatter   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 1

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 110
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A formatListing() 0 11 1
A addPathInfo() 0 4 1
A isEntryOutOfScope() 0 12 4
A residesInDirectory() 0 8 2
A isDirectChild() 0 4 1
A sortListing() 0 11 1
1
<?php
2
3
namespace League\Flysystem\Util;
4
5
use League\Flysystem\Util;
6
7
/**
8
 * @internal
9
 */
10
class ContentListingFormatter
11
{
12
    /**
13
     * @var string
14
     */
15
    private $directory;
16
    /**
17
     * @var bool
18
     */
19
    private $recursive;
20
21
    /**
22
     * @param string $directory
23
     * @param bool   $recursive
24
     */
25 12
    public function __construct($directory, $recursive)
26
    {
27 12
        $this->directory = $directory;
28 12
        $this->recursive = $recursive;
29 12
    }
30
31
    /**
32
     * Format contents listing.
33
     *
34
     * @param array $listing
35
     *
36
     * @return array
37
     */
38 12
    public function formatListing(array $listing)
39
    {
40 12
        $listing = array_values(
41 12
            array_map(
42 12
                [$this, 'addPathInfo'],
43 12
                array_filter($listing, [$this, 'isEntryOutOfScope'])
44 8
            )
45 8
        );
46
47 12
        return $this->sortListing($listing);
48
    }
49
50 9
    private function addPathInfo(array $entry)
51
    {
52 9
        return $entry + Util::pathinfo($entry['path']);
53
    }
54
55
    /**
56
     * Determine if the entry is out of scope.
57
     *
58
     * @param array $entry
59
     *
60
     * @return bool
61
     */
62 12
    private function isEntryOutOfScope(array $entry)
63
    {
64 12
        if (empty($entry['path']) && $entry['path'] !== '0') {
65 3
            return false;
66
        }
67
68 12
        if ($this->recursive) {
69 6
            return $this->residesInDirectory($entry);
70
        }
71
72 6
        return $this->isDirectChild($entry);
73
    }
74
75
    /**
76
     * Check if the entry resides within the parent directory.
77
     *
78
     * @param $entry
79
     *
80
     * @return bool
81
     */
82 6
    private function residesInDirectory(array $entry)
83
    {
84 6
        if ($this->directory === '') {
85 3
            return true;
86
        }
87
88 6
        return strpos($entry['path'], $this->directory . '/') === 0;
89
    }
90
91
    /**
92
     * Check if the entry is a direct child of the directory.
93
     *
94
     * @param $entry
95
     *
96
     * @return bool
97
     */
98 6
    private function isDirectChild(array $entry)
99
    {
100 6
        return Util::dirname($entry['path']) === $this->directory;
101
    }
102
103
    /**
104
     * @param array $listing
105
     *
106
     * @return array
107
     */
108 12
    private function sortListing(array $listing)
109
    {
110 12
        usort(
111 12
            $listing,
112 10
            function ($a, $b) {
113 9
                return strcasecmp($a['path'], $b['path']);
114 4
            }
115 8
        );
116
117 12
        return $listing;
118
    }
119
}
120