Passed
Pull Request — master (#409)
by korelstar
01:53
created

Note::updateETag()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
c 0
b 0
f 0
rs 10
cc 3
nc 3
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
			if ($fileContent===false) {
0 ignored issues
show
introduced by
The condition $fileContent === false is always false.
Loading history...
56
				throw new \Exception("File not found");
57
			}
58
			$note->setContent(self::convertEncoding($fileContent));
59
		}
60
		$note->setModified($file->getMTime());
61
		if (!$onlyMeta) {
62
			$note->updateETag();
63
		}
64
		$note->resetUpdatedFields();
65
		return $note;
66
	}
67
68
	/**
69
	 * @param File $file
70
	 * @return static
71
	 */
72
	public static function fromException($message, File $file, Folder $notesFolder, $tags = []) {
73
		$note = new static();
74
		$note->initCommonBaseFields($file, $notesFolder, $tags);
75
		$note->setErrorMessage($message);
76
		$note->setError(true);
77
		$note->setContent($message);
78
		$note->setModified(null);
79
		$note->resetUpdatedFields();
80
		return $note;
81
	}
82
83
	private static function convertEncoding($str) {
84
		if (!mb_check_encoding($str, 'UTF-8')) {
85
			$str = mb_convert_encoding($str, 'UTF-8');
86
		}
87
		return $str;
88
	}
89
90
	private function initCommonBaseFields(File $file, Folder $notesFolder, $tags) {
91
		$this->setId($file->getId());
92
		$this->setTitle(pathinfo($file->getName(), PATHINFO_FILENAME)); // remove extension
93
		$subdir = substr(dirname($file->getPath()), strlen($notesFolder->getPath())+1);
94
		$this->setCategory($subdir ? $subdir : '');
95
		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...
96
			$this->setFavorite(true);
97
			//unset($tags[array_search(\OC\Tags::TAG_FAVORITE, $tags)]);
98
		}
99
	}
100
101
	private function updateETag() {
102
		// collect all relevant attributes
103
		$data = '';
104
		foreach (get_object_vars($this) as $key => $val) {
105
			if ($key!=='etag') {
106
				$data .= $val;
107
			}
108
		}
109
		$etag = md5($data);
110
		$this->setEtag($etag);
111
	}
112
}
113