Completed
Push — 1.0 ( de14e4...537877 )
by Bernhard
57:08 queued 17:45
created

DirectoryResource   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 15
c 3
b 1
f 1
lcom 1
cbo 7
dl 0
loc 99
ccs 38
cts 38
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getChild() 0 19 5
A hasChild() 0 9 2
A hasChildren() 0 15 2
B listChildren() 0 28 5
1
<?php
2
3
/*
4
 * This file is part of the puli/repository package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Repository\Resource;
13
14
use Puli\Repository\Api\ResourceNotFoundException;
15
use Puli\Repository\Resource\Collection\FilesystemResourceCollection;
16
use Webmozart\Assert\Assert;
17
use Webmozart\Glob\Iterator\RecursiveDirectoryIterator;
18
19
/**
20
 * Represents a directory on the file system.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class DirectoryResource extends AbstractFilesystemResource
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31 427
    public function __construct($filesystemPath, $path = null)
32
    {
33 427
        Assert::directory($filesystemPath);
34
35 424
        parent::__construct($filesystemPath, $path);
36 424
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 7
    public function getChild($relPath)
42
    {
43
        // Use attached repository if possible
44 7
        if ($this->getRepository()) {
45 2
            return $this->getRepository()->get($this->getRepositoryPath().'/'.$relPath);
46
        }
47
48 5
        $filesystemPath = $this->getFilesystemPath().'/'.$relPath;
49
50 5
        if (!file_exists($filesystemPath)) {
51
            throw ResourceNotFoundException::forPath($this->getPath().'/'.$relPath);
52
        }
53
54 5
        $childPath = null === $this->getPath() ? null : $this->getPath().'/'.$relPath;
55
56 5
        return is_dir($filesystemPath)
57
            ? new self($filesystemPath, $childPath)
58 5
            : new FileResource($filesystemPath, $childPath);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 3
    public function hasChild($relPath)
65
    {
66
        // Use attached repository if possible
67 3
        if ($this->getRepository()) {
68 2
            return $this->getRepository()->contains($this->getRepositoryPath().'/'.$relPath);
69
        }
70
71 1
        return file_exists($this->getFilesystemPath().'/'.$relPath);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 36
    public function hasChildren()
78
    {
79
        // Use attached repository if possible
80 36
        if ($this->getRepository()) {
81 10
            return $this->getRepository()->hasChildren($this->getRepositoryPath());
82
        }
83
84 26
        $iterator = new RecursiveDirectoryIterator(
85 26
            $this->getFilesystemPath(),
86 26
            RecursiveDirectoryIterator::SKIP_DOTS
87
        );
88 26
        $iterator->rewind();
89
90 26
        return $iterator->valid();
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 142
    public function listChildren()
97
    {
98 142
        $children = new FilesystemResourceCollection();
99
100
        // Use attached repository if possible
101 142
        if ($this->getRepository()) {
102 23
            foreach ($this->getRepository()->listChildren($this->getRepositoryPath()) as $child) {
103 19
                $children[$child->getName()] = $child;
104
            }
105
106 23
            return $children;
107
        }
108
109 126
        $iterator = new RecursiveDirectoryIterator(
110 126
            $this->getFilesystemPath(),
111 126
            RecursiveDirectoryIterator::CURRENT_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS
112
        );
113
114
        // We can't use glob() here, because glob() doesn't list files starting
115
        // with "." by default
116 126
        foreach ($iterator as $path) {
117 80
            $children[basename($path)] = is_dir($path)
118 68
                ? new self($path)
119 80
                : new FileResource($path);
120
        }
121
122 126
        return $children;
123
    }
124
}
125