Test Failed
Push — master ( 952eb6...02e784 )
by Daniel
24:52
created

PicoAsset::getContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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\Model;
26
27
use OCA\CMSPico\Files\StorageFile;
28
use OCP\Files\NotPermittedException;
29
30
class PicoAsset
31
{
32
	/** @var StorageFile */
33
	private $file;
34
35
	/** @var bool */
36
	private $publicAsset;
37
38
	/** @var \DateTime|null */
39
	private $lastModified;
40
41
	/** @var string|null */
42
	private $secureMimeType;
43
44
	/**
45
	 * Asset constructor.
46
	 *
47
	 * @param StorageFile $file
48
	 * @param bool        $publicAsset
49
	 *
50
	 * @throws NotPermittedException
51
	 */
52
	public function __construct(StorageFile $file, bool $publicAsset)
53
	{
54
		$this->file = $file;
55
		$this->publicAsset = $publicAsset;
56
57
		if (!$this->isReadable()) {
58
			throw new NotPermittedException();
59
		}
60
	}
61
62
	/**
63
	 * @return string
64
	 */
65
	public function getPath(): string
66
	{
67
		return $this->file->getPath();
68
	}
69
70
	/**
71
	 * @return string
72
	 */
73
	public function getName(): string
74
	{
75
		return $this->file->getName();
76
	}
77
78
	/**
79
	 * @return string
80
	 */
81
	public function getExtension(): string
82
	{
83
		return $this->file->getExtension();
84
	}
85
86
	/**
87
	 * @return string
88
	 */
89
	public function getETag(): string
90
	{
91
		return $this->file->getOCNode()->getEtag();
92
	}
93
94
	/**
95
	 * @return \DateTime
96
	 */
97
	public function getLastModified(): \DateTime
98
	{
99
		if ($this->lastModified === null) {
100
			$lastModifiedTime = $this->file->getOCNode()->getMTime();
101
			$this->lastModified = (new \DateTime())->setTimestamp($lastModifiedTime);
102
		}
103
104
		return $this->lastModified;
105
	}
106
107
	/**
108
	 * @return string
109
	 */
110
	public function getMimeType(): string
111
	{
112
		return $this->file->getOCNode()->getMimetype() ?: 'application/octet-stream';
113
	}
114
115
	/**
116
	 * @return string
117
	 */
118
	public function getSecureMimeType(): string
119
	{
120
		if ($this->secureMimeType === null) {
121
			$mimeTypeDetector = \OC::$server->getMimeTypeDetector();
122
			$this->secureMimeType = $mimeTypeDetector->getSecureMimeType($this->getMimeType());
123
		}
124
125
		return $this->secureMimeType;
126
	}
127
128
	/**
129
	 * @return bool
130
	 */
131
	public function isReadable(): bool
132
	{
133
		return $this->file->isReadable();
134
	}
135
136
	/**
137
	 * @return bool
138
	 */
139
	public function isPublicAsset(): bool
140
	{
141
		return $this->publicAsset;
142
	}
143
144
	/**
145
	 * @return string
146
	 */
147
	public function getContent(): string
148
	{
149
		return $this->file->getContent();
150
	}
151
}
152