Test Setup Failed
Push — master ( 4a93bf...5a5f5f )
by Daniel
23:25
created

PicoAsset   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 78.13%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 25
c 1
b 0
f 0
dl 0
loc 120
ccs 25
cts 32
cp 0.7813
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 3 1
A getExtension() 0 3 1
A getName() 0 3 1
A getLastModified() 0 8 2
A getSecureMimeType() 0 8 2
A getContent() 0 3 1
A isPublicAsset() 0 3 1
A getMimeType() 0 3 2
A getETag() 0 3 1
A isReadable() 0 3 1
A __construct() 0 7 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 2
	public function __construct(StorageFile $file, bool $publicAsset)
53
	{
54 2
		$this->file = $file;
55 2
		$this->publicAsset = $publicAsset;
56
57 2
		if (!$this->isReadable()) {
58
			throw new NotPermittedException();
59
		}
60 2
	}
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 2
	public function getName(): string
74
	{
75 2
		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 2
	public function getETag(): string
90
	{
91 2
		return $this->file->getOCNode()->getEtag();
92
	}
93
94
	/**
95
	 * @return \DateTime
96
	 */
97 2
	public function getLastModified(): \DateTime
98
	{
99 2
		if ($this->lastModified === null) {
100 2
			$lastModifiedTime = $this->file->getOCNode()->getMTime();
101 2
			$this->lastModified = (new \DateTime())->setTimestamp($lastModifiedTime);
102
		}
103
104 2
		return $this->lastModified;
105
	}
106
107
	/**
108
	 * @return string
109
	 */
110 2
	public function getMimeType(): string
111
	{
112 2
		return $this->file->getOCNode()->getMimetype() ?: 'application/octet-stream';
113
	}
114
115
	/**
116
	 * @return string
117
	 */
118 2
	public function getSecureMimeType(): string
119
	{
120 2
		if ($this->secureMimeType === null) {
121 2
			$mimeTypeDetector = \OC::$server->getMimeTypeDetector();
0 ignored issues
show
Deprecated Code introduced by
The function OC\Server::getMimeTypeDetector() has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

121
			$mimeTypeDetector = /** @scrutinizer ignore-deprecated */ \OC::$server->getMimeTypeDetector();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
122 2
			$this->secureMimeType = $mimeTypeDetector->getSecureMimeType($this->getMimeType());
123
		}
124
125 2
		return $this->secureMimeType;
126
	}
127
128
	/**
129
	 * @return bool
130
	 */
131 2
	public function isReadable(): bool
132
	{
133 2
		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 1
	public function getContent(): string
148
	{
149 1
		return $this->file->getContent();
150
	}
151
}
152