Passed
Push — master ( 235c52...927bab )
by Daniel
30:30 queued 10s
created

FolderTrait::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace OCA\CMSPico\Files;
26
27
use OCP\Files\GenericFileException;
28
use OCP\Files\InvalidPathException;
29
use OCP\Files\NotFoundException;
30
use OCP\Files\NotPermittedException;
31
32
trait FolderTrait
33
{
34
	/** @var \Generator */
35
	private $generator;
36
37
	/**
38
	 * @param string $path
39
	 *
40
	 * @return FolderInterface
41
	 * @throws InvalidPathException
42
	 * @throws NotFoundException
43
	 * @throws GenericFileException
44
	 */
45 7
	public function getFolder(string $path): FolderInterface
46
	{
47
		/** @var FolderInterface $folder */
48 7
		$folder = $this->get($path);
49 7
		if (!$folder->isFolder()) {
50
			throw new InvalidPathException();
51
		}
52
53 7
		return $folder;
54
	}
55
56
	/**
57
	 * @param string $path
58
	 *
59
	 * @return FileInterface
60
	 * @throws InvalidPathException
61
	 * @throws NotFoundException
62
	 * @throws GenericFileException
63
	 */
64 3
	public function getFile(string $path): FileInterface
65
	{
66
		/** @var FileInterface $file */
67 3
		$file = $this->get($path);
68 3
		if (!$file->isFile()) {
69
			throw new InvalidPathException();
70
		}
71
72 3
		return $file;
73
	}
74
75
	/**
76
	 * @return \Generator
77
	 * @throws NotPermittedException
78
	 * @throws GenericFileException
79
	 */
80
	abstract protected function getGenerator(): \Generator;
81
82
	/**
83
	 * @return void
84
	 * @throws NotPermittedException
85
	 * @throws GenericFileException
86
	 */
87 5
	public function rewind()
88
	{
89 5
		$this->generator = $this->getGenerator();
90 5
	}
91
92
	/**
93
	 * @return void
94
	 */
95 5
	public function next()
96
	{
97 5
		$this->generator->next();
98 5
	}
99
100
	/**
101
	 * @return bool
102
	 */
103 5
	public function valid(): bool
104
	{
105 5
		return $this->generator->valid();
106
	}
107
108
	/**
109
	 * @return NodeInterface
110
	 */
111 5
	public function current(): NodeInterface
112
	{
113 5
		return $this->generator->current();
114
	}
115
116
	/**
117
	 * @return int
118
	 */
119
	public function key(): int
120
	{
121
		return $this->generator->key();
122
	}
123
124
	/**
125
	 * @return bool
126
	 */
127 3
	public function hasChildren(): bool
128
	{
129 3
		return ($this->current() instanceof FolderInterface);
130
	}
131
132
	/**
133
	 * @return \RecursiveIterator
134
	 */
135 3
	public function getChildren(): \RecursiveIterator
136
	{
137 3
		$node = $this->current();
138 3
		if ($node instanceof FolderInterface) {
139
			/** @var FolderInterface $node */
140 3
			return $node;
141
		}
142
143
		throw new \InvalidArgumentException();
144
	}
145
}
146