Completed
Pull Request — 1.0 (#82)
by Bernhard
20:24 queued 11s
created

DirectoryResource::hasChild()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2
Metric Value
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 9.6667
cc 2
eloc 4
nc 2
nop 1
crap 2
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 411
    public function __construct($filesystemPath, $path = null)
32
    {
33 411
        Assert::directory($filesystemPath);
34
35 408
        parent::__construct($filesystemPath, $path);
36 408
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 3
    public function getChild($relPath)
42
    {
43
        // Use attached repository if possible
44 3
        if ($this->getRepository()) {
45 2
            return $this->getRepository()->get($this->getRepositoryPath().'/'.$relPath);
46
        }
47
48 1
        $filesystemPath = $this->getFilesystemPath().'/'.$relPath;
49
50 1
        if (!file_exists($filesystemPath)) {
51
            throw ResourceNotFoundException::forPath($this->getPath().'/'.$relPath);
52
        }
53
54 1
        $childPath = null === $this->getPath() ? null : $this->getPath().'/'.$relPath;
55 1
56 1
        return is_dir($filesystemPath)
57
            ? new self($filesystemPath, $childPath)
58
            : new FileResource($filesystemPath, $childPath);
59
    }
60
61
    /**
62 3
     * {@inheritdoc}
63
     */
64
    public function hasChild($relPath)
65 3
    {
66 2
        // Use attached repository if possible
67
        if ($this->getRepository()) {
68
            return $this->getRepository()->contains($this->getRepositoryPath().'/'.$relPath);
69 1
        }
70
71
        return file_exists($this->getFilesystemPath().'/'.$relPath);
72
    }
73
74
    /**
75 36
     * {@inheritdoc}
76
     */
77
    public function hasChildren()
78 36
    {
79 10
        // Use attached repository if possible
80
        if ($this->getRepository()) {
81
            return $this->getRepository()->hasChildren($this->getRepositoryPath());
82 26
        }
83 26
84
        $iterator = new RecursiveDirectoryIterator(
85 26
            $this->getFilesystemPath(),
86 26
            RecursiveDirectoryIterator::SKIP_DOTS
87
        );
88 26
        $iterator->rewind();
89
90
        return $iterator->valid();
91
    }
92
93
    /**
94 136
     * {@inheritdoc}
95
     */
96 136
    public function listChildren()
97
    {
98
        $children = new FilesystemResourceCollection();
99 136
100 21
        // Use attached repository if possible
101 17
        if ($this->getRepository()) {
102 21
            foreach ($this->getRepository()->listChildren($this->getRepositoryPath()) as $child) {
103
                $children[$child->getName()] = $child;
104 21
            }
105
106
            return $children;
107 120
        }
108 120
109 120
        $iterator = new RecursiveDirectoryIterator(
110 120
            $this->getFilesystemPath(),
111
            RecursiveDirectoryIterator::CURRENT_AS_PATHNAME | RecursiveDirectoryIterator::SKIP_DOTS
112
        );
113
114 120
        // We can't use glob() here, because glob() doesn't list files starting
115 74
        // with "." by default
116 74
        foreach ($iterator as $path) {
117 74
            $children[basename($path)] = is_dir($path)
118 120
                ? new self($path)
119
                : new FileResource($path);
120 120
        }
121
122
        return $children;
123
    }
124
}
125