Completed
Push — master ( 487a10...ed38f8 )
by korelstar
03:25 queued 02:02
created

Note::updateETag()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
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
use League\Flysystem\FileNotFoundException;
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
 * @method boolean getError()
35
 * @method void setError(boolean $value)
36
 * @method string getErrorMessage()
37
 * @method void setErrorMessage(string $value)
38
 * @package OCA\Notes\Db
39
 */
40
class Note extends Entity {
41
    public $etag;
42
    public $modified;
43
    public $title;
44
    public $category;
45
    public $content;
46
    public $favorite = false;
47
    public $error = false;
48
    public $errorMessage='';
49
50
    public function __construct() {
51
        $this->addType('modified', 'integer');
52
        $this->addType('favorite', 'boolean');
53
    }
54
55
    /**
56
     * @param File $file
57
     * @return static
58
     */
59
    public static function fromFile(File $file, Folder $notesFolder, $tags=[], $onlyMeta=false) {
60
        $note = new static();
61
        $note->setId($file->getId());
62
        if(!$onlyMeta) {
63
            $fileContent=$file->getContent();
64
            if($fileContent===false){
65
                throw new FileNotFoundException("File not found");
66
            }
67
            $note->setContent(self::convertEncoding($fileContent));
68
        }
69
        $note->setModified($file->getMTime());
70
        $note->setTitle(pathinfo($file->getName(),PATHINFO_FILENAME)); // remove extension
71
        $subdir = substr(dirname($file->getPath()), strlen($notesFolder->getPath())+1);
72
        $note->setCategory($subdir ? $subdir : null);
73
        if(is_array($tags) && in_array(\OC\Tags::TAG_FAVORITE, $tags)) {
74
            $note->setFavorite(true);
75
            //unset($tags[array_search(\OC\Tags::TAG_FAVORITE, $tags)]);
76
        }
77
        if(!$onlyMeta) {
78
            $note->updateETag();
79
        }
80
        $note->resetUpdatedFields();
81
        return $note;
82
    }
83
    /**
84
     * @param File $file
85
     * @return static
86
     */
87
    public static function fromException($message,File $file,Folder $notesFolder,$tags=[]){
88
        $note = new static();
89
        $note->setId($file->getId());
90
        $note->setErrorMessage($message);
91
        $note->setError(true);
92
        $note->setContent($message);
93
        $note->setModified(null);
94
        $note->setTitle(pathinfo($file->getName(),PATHINFO_FILENAME)); // remove extension
95
        $subdir = substr(dirname($file->getPath()), strlen($notesFolder->getPath())+1);
96
        $note->setCategory($subdir ? $subdir : null);
97
        if(is_array($tags) && in_array(\OC\Tags::TAG_FAVORITE, $tags)) {
98
            $note->setFavorite(true);
99
            //unset($tags[array_search(\OC\Tags::TAG_FAVORITE, $tags)]);
100
        }
101
        $note->updateETag();
102
        $note->resetUpdatedFields();
103
        return $note;
104
    }
105
106
    private static function convertEncoding($str) {
107
        if(!mb_check_encoding($str, 'UTF-8')) {
108
            $str = mb_convert_encoding($str, 'UTF-8');
109
        }
110
        return $str;
111
    }
112
113
    private function updateETag() {
114
        // collect all relevant attributes
115
        $data = '';
116
        foreach(get_object_vars($this) as $key => $val) {
117
            if($key!=='etag') {
118
                $data .= $val;
119
            }
120
        }
121
        $etag = md5($data);
122
        $this->setEtag($etag);
123
    }
124
}
125