Passed
Push — master ( eef64e...21c33e )
by korelstar
01:59
created

Note::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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
	public function __construct() {
42
		$this->addType('modified', 'integer');
43
		$this->addType('favorite', 'boolean');
44
	}
45
46
	/**
47
	 * @param File $file
48
	 * @return static
49
	 */
50
	public static function fromFile(File $file, Folder $notesFolder, $tags = [], $onlyMeta = false) {
51
		$note = new static();
52
		$note->initCommonBaseFields($file, $notesFolder, $tags);
53
		if (!$onlyMeta) {
54
			$fileContent=$file->getContent();
55
			$note->setContent(self::convertEncoding($fileContent));
56
		}
57
		$note->setModified($file->getMTime());
58
		if (!$onlyMeta) {
59
			$note->updateETag();
60
		}
61
		$note->resetUpdatedFields();
62
		return $note;
63
	}
64
65
	/**
66
	 * @param File $file
67
	 * @return static
68
	 */
69
	public static function fromException($message, File $file, Folder $notesFolder, $tags = []) {
70
		$note = new static();
71
		$note->initCommonBaseFields($file, $notesFolder, $tags);
72
		$note->setErrorMessage($message);
73
		$note->setError(true);
74
		$note->setContent($message);
75
		$note->setModified(null);
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
		$subdir = substr(dirname($file->getPath()), strlen($notesFolder->getPath())+1);
91
		$this->setCategory($subdir ? $subdir : '');
92
		if (is_array($tags) && in_array(\OC\Tags::TAG_FAVORITE, $tags)) {
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...
93
			$this->setFavorite(true);
94
			//unset($tags[array_search(\OC\Tags::TAG_FAVORITE, $tags)]);
95
		}
96
	}
97
98
	private function updateETag() {
99
		// collect all relevant attributes
100
		$data = '';
101
		foreach (get_object_vars($this) as $key => $val) {
102
			if ($key!=='etag') {
103
				$data .= $val;
104
			}
105
		}
106
		$etag = md5($data);
107
		$this->setEtag($etag);
108
	}
109
}
110