Passed
Push — master ( 695eed...e372a6 )
by Daniel
23:18
created

LocalFile::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
ccs 3
cts 5
cp 0.6
rs 10
cc 3
nc 3
nop 0
crap 3.576
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
class LocalFile extends AbstractLocalNode implements FileInterface
33
{
34
	/**
35
	 * LocalFile constructor.
36
	 *
37
	 * @param string $path
38
	 * @param string $basePath
39
	 *
40
	 * @throws InvalidPathException
41
	 * @throws NotFoundException
42
	 */
43 13
	public function __construct(string $path, string $basePath)
44
	{
45 13
		parent::__construct($path, $basePath);
46
47 13
		if (!is_file($this->getLocalPath())) {
48
			throw new InvalidPathException();
49
		}
50 13
	}
51
52
	/**
53
	 * {@inheritDoc}
54
	 */
55
	public function getExtension(): string
56
	{
57
		$pos = strrpos($this->getName(), '.');
58
		return ($pos !== false) ? substr($this->getName(), $pos + 1) : '';
59
	}
60
61
	/**
62
	 * {@inheritDoc}
63
	 */
64 5
	public function getContent(): string
65
	{
66 5
		if (!$this->isReadable()) {
67
			throw new NotPermittedException();
68
		}
69
70 5
		if (($data = @file_get_contents($this->getLocalPath())) === false) {
71
			throw new GenericFileException();
72
		}
73
74 5
		return $data;
75
	}
76
77
	/**
78
	 * {@inheritDoc}
79
	 */
80 7
	public function putContent(string $data): void
81
	{
82 7
		if (!$this->isUpdateable()) {
83
			throw new NotPermittedException();
84
		}
85
86 7
		if (@file_put_contents($this->getLocalPath(), $data) === false) {
87
			throw new GenericFileException();
88
		}
89 7
	}
90
}
91