Passed
Pull Request — master (#77)
by Daniel
28:07
created

PicoAsset::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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