Passed
Push — master ( 4bdb47...6c7738 )
by Daniel
25:23 queued 10s
created

LocalFile   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 19
c 1
b 0
f 0
dl 0
loc 70
ccs 15
cts 25
cp 0.6
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A putContent() 0 8 3
A getExtension() 0 4 2
A getContent() 0 11 3
A delete() 0 8 3
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