Completed
Pull Request — master (#95)
by korelstar
05:25
created

Note::updateETag()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
/**
3
 * Nextcloud - Notes
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Bernhard Posselt <[email protected]>
9
 * @copyright Bernhard Posselt 2012, 2014
10
 */
11
12
namespace OCA\Notes\Db;
13
14
use OCP\Files\File;
15
use OCP\Files\Folder;
16
use OCP\AppFramework\Db\Entity;
17
18
/**
19
 * Class Note
20
 * @method integer getId()
21
 * @method void setId(integer $value)
22
 * @method string getEtag()
23
 * @method void setEtag(string $value)
24
 * @method integer getModified()
25
 * @method void setModified(integer $value)
26
 * @method string getTitle()
27
 * @method void setTitle(string $value)
28
 * @method string getCategory()
29
 * @method void setCategory(string $value)
30
 * @method string getContent()
31
 * @method void setContent(string $value)
32
 * @method boolean getFavorite()
33
 * @method void setFavorite(boolean $value)
34
 * @package OCA\Notes\Db
35
 */
36
class Note extends Entity {
37
38
    public $etag;
39
    public $modified;
40
    public $title;
41
    public $category;
42
    public $content;
43
    public $favorite = false;
44
45
    public function __construct() {
46
        $this->addType('modified', 'integer');
47
        $this->addType('favorite', 'boolean');
48
    }
49
50
    /**
51
     * @param File $file
52
     * @return static
53
     */
54
    public static function fromFile(File $file, Folder $notesFolder, $tags=[]){
55
        $note = new static();
56
        $note->setId($file->getId());
57
        $note->setContent(self::convertEncoding($file->getContent()));
58
        $note->setModified($file->getMTime());
59
        $note->setTitle(pathinfo($file->getName(),PATHINFO_FILENAME)); // remove extension
60
        $subdir = substr(dirname($file->getPath()), strlen($notesFolder->getPath())+1);
61
        $note->setCategory($subdir ? $subdir : null);
62
        if(is_array($tags) && in_array(\OC\Tags::TAG_FAVORITE, $tags)) {
63
            $note->setFavorite(true);
64
            //unset($tags[array_search(\OC\Tags::TAG_FAVORITE, $tags)]);
65
        }
66
        $note->updateETag();
67
        $note->resetUpdatedFields();
68
        return $note;
69
    }
70
71
    private static function convertEncoding($str) {
72
        if(!mb_check_encoding($str, 'UTF-8')) {
73
            $str = mb_convert_encoding($str, 'UTF-8');
74
        }
75
        return $str;
76
    }
77
78
    private function updateETag() {
79
        // collect all relevant attributes
80
        $data = '';
81
        foreach(get_object_vars($this) as $key => $val) {
82
            if($key!=='etag') {
83
                $data .= $val;
84
            }
85
        }
86
        $etag = md5($data);
87
        $this->setEtag($etag);
88
    }
89
}
90