Passed
Push — master ( 4bdb47...6c7738 )
by Daniel
25:23 queued 10s
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|null $rootPath
39
	 *
40
	 * @throws InvalidPathException
41
	 * @throws NotFoundException
42
	 */
43 13
	public function __construct(string $path, string $rootPath = null)
44
	{
45 13
		parent::__construct($path, $rootPath);
46
47 13
		if (!is_file($this->getLocalPath())) {
48
			throw new InvalidPathException();
49
		}
50 13
	}
51
52
	/**
53
	 * {@inheritDoc}
54
	 */
55 4
	public function delete(): void
56
	{
57 4
		if (!$this->isDeletable()) {
58
			throw new NotPermittedException();
59
		}
60
61 4
		if (!@unlink($this->getLocalPath())) {
62
			throw new GenericFileException();
63
		}
64 4
	}
65
66
	/**
67
	 * {@inheritDoc}
68
	 */
69
	public function getExtension(): string
70
	{
71
		$pos = strrpos($this->getName(), '.');
72
		return ($pos !== false) ? substr($this->getName(), $pos + 1) : '';
73
	}
74
75
	/**
76
	 * {@inheritDoc}
77
	 */
78 5
	public function getContent(): string
79
	{
80 5
		if (!$this->isReadable()) {
81
			throw new NotPermittedException();
82
		}
83
84 5
		if (($data = @file_get_contents($this->getLocalPath())) === false) {
85
			throw new GenericFileException();
86
		}
87
88 5
		return $data;
89
	}
90
91
	/**
92
	 * {@inheritDoc}
93
	 */
94 7
	public function putContent(string $data): void
95
	{
96 7
		if (!$this->isUpdateable()) {
97
			throw new NotPermittedException();
98
		}
99
100 7
		if (@file_put_contents($this->getLocalPath(), $data) === false) {
101
			throw new GenericFileException();
102
		}
103 7
	}
104
}
105