Note::fromException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
namespace OCA\Notes\Db;
4
5
use OCP\Files\File;
6
use OCP\Files\Folder;
7
use OCP\AppFramework\Db\Entity;
8
9
/**
10
 * Class Note
11
 * @method integer getId()
12
 * @method void setId(integer $value)
13
 * @method string getEtag()
14
 * @method void setEtag(string $value)
15
 * @method integer getModified()
16
 * @method void setModified(integer $value)
17
 * @method string getTitle()
18
 * @method void setTitle(string $value)
19
 * @method string getCategory()
20
 * @method void setCategory(string $value)
21
 * @method string getContent()
22
 * @method void setContent(string $value)
23
 * @method boolean getFavorite()
24
 * @method void setFavorite(boolean $value)
25
 * @method boolean getError()
26
 * @method void setError(boolean $value)
27
 * @method string getErrorMessage()
28
 * @method void setErrorMessage(string $value)
29
 * @package OCA\Notes\Db
30
 */
31
class Note extends Entity {
32
	public $etag;
33
	public $modified;
34
	public $title;
35
	public $category;
36
	public $content = null;
37
	public $favorite = false;
38
	public $error = false;
39
	public $errorMessage='';
40
41
	private const TAG_FAVORITE = \OC\Tags::TAG_FAVORITE; // @phan-suppress-current-line PhanUndeclaredClassConstant
0 ignored issues
show
Bug introduced by
The type OC\Tags was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
43
	public function __construct() {
44
		$this->addType('modified', 'integer');
45
		$this->addType('favorite', 'boolean');
46
	}
47
48
	/**
49
	 * @param File $file
50
	 * @return static
51
	 */
52
	public static function fromFile(File $file, Folder $notesFolder, $tags = [], $onlyMeta = false) {
53
		$note = new static();
54
		$note->initCommonBaseFields($file, $notesFolder, $tags);
55
		if (!$onlyMeta) {
56
			$fileContent=$file->getContent();
57
			$note->setContent(self::convertEncoding($fileContent));
58
		}
59
		if (!$onlyMeta) {
60
			$note->updateETag();
61
		}
62
		$note->resetUpdatedFields();
63
		return $note;
64
	}
65
66
	/**
67
	 * @param File $file
68
	 * @return static
69
	 */
70
	public static function fromException($message, File $file, Folder $notesFolder, $tags = []) {
71
		$note = new static();
72
		$note->initCommonBaseFields($file, $notesFolder, $tags);
73
		$note->setErrorMessage($message);
74
		$note->setError(true);
75
		$note->setContent($message);
76
		$note->resetUpdatedFields();
77
		return $note;
78
	}
79
80
	private static function convertEncoding($str) {
81
		if (!mb_check_encoding($str, 'UTF-8')) {
82
			$str = mb_convert_encoding($str, 'UTF-8');
83
		}
84
		return $str;
85
	}
86
87
	private function initCommonBaseFields(File $file, Folder $notesFolder, $tags) {
88
		$this->setId($file->getId());
89
		$this->setTitle(pathinfo($file->getName(), PATHINFO_FILENAME)); // remove extension
90
		$this->setModified($file->getMTime());
91
		$subdir = substr(dirname($file->getPath()), strlen($notesFolder->getPath())+1);
92
		$this->setCategory($subdir ? $subdir : '');
93
		if (is_array($tags) && in_array(self::TAG_FAVORITE, $tags)) {
94
			$this->setFavorite(true);
95
			//unset($tags[array_search(self::TAG_FAVORITE, $tags)]);
96
		}
97
	}
98
99
	private function updateETag() {
100
		// collect all relevant attributes
101
		$data = '';
102
		foreach (get_object_vars($this) as $key => $val) {
103
			if ($key!=='etag') {
104
				$data .= $val;
105
			}
106
		}
107
		$etag = md5($data);
108
		$this->setEtag($etag);
109
	}
110
}
111