Completed
Push — integration-test-cleanup ( fb0c79...570c75 )
by
unknown
09:33
created

Item   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 24
c 4
b 0
f 2
lcom 2
cbo 1
dl 0
loc 202
ccs 134
cts 134
cp 1
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A setRead() 0 4 1
A isRead() 0 3 1
A setUnread() 0 4 1
A isUnread() 0 3 1
A setStarred() 0 4 1
A isStarred() 0 3 1
A setUnstarred() 0 4 1
A isUnstarred() 0 3 1
A __construct() 0 7 1
A setAuthor() 0 3 1
A setTitle() 0 3 1
A jsonSerialize() 0 20 1
A toAPI() 0 19 1
A toExport() 0 16 1
A getIntro() 0 3 1
B fromImport() 0 25 3
A generateSearchIndex() 0 11 1
A computeFingerprint() 0 4 1
A setUrl() 0 6 3
A setBody() 0 7 1
1
<?php
2
/**
3
 * ownCloud - News
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Alessandro Cosentino <[email protected]>
9
 * @author Bernhard Posselt <[email protected]>
10
 * @copyright Alessandro Cosentino 2012
11
 * @copyright Bernhard Posselt 2012, 2014
12
 */
13
14
namespace OCA\News\Db;
15
16
use \OCP\AppFramework\Db\Entity;
17
18
19
/**
20
 * @method integer getId()
21
 * @method void setId(integer $value)
22
 * @method string getGuid()
23
 * @method void setGuid(string $value)
24
 * @method string getGuidHash()
25
 * @method void setGuidHash(string $value)
26
 * @method string getGuid()
27
 * @method string getUrl()
28
 * @method string getTitle()
29
 * @method string getAuthor()
30
 * @method string getRtl()
31
 * @method string getFingerprint()
32
 * @method integer getPubDate()
33
 * @method void setPubDate(integer $value)
34
 * @method string getBody()
35
 * @method string getEnclosureMime()
36
 * @method void setEnclosureMime(string $value)
37
 * @method string getEnclosureLink()
38
 * @method void setEnclosureLink(string $value)
39
 * @method integer getFeedId()
40
 * @method void setFeedId(integer $value)
41
 * @method integer getStatus()
42
 * @method void setStatus(integer $value)
43
 * @method void setRtl(boolean $value)
44
 * @method integer getLastModified()
45
 * @method void setLastModified(integer $value)
46
 * @method void setFingerprint(string $value)
47
 * @method void setSearchIndex(string $value)
48
 */
49
class Item extends Entity implements IAPI, \JsonSerializable {
50
51
    use EntityJSONSerializer;
52
53
    protected $guidHash;
54
    protected $guid;
55
    protected $url;
56
    protected $title;
57
    protected $author;
58
    protected $pubDate;
59
    protected $body;
60
    protected $enclosureMime;
61
    protected $enclosureLink;
62
    protected $feedId;
63
    protected $status = 0;
64
    protected $lastModified;
65
    protected $searchIndex;
66
    protected $rtl;
67
    protected $fingerprint;
68
69 82
    public function __construct(){
70 82
        $this->addType('pubDate', 'integer');
71 82
        $this->addType('feedId', 'integer');
72 82
        $this->addType('status', 'integer');
73 82
        $this->addType('lastModified', 'integer');
74 82
        $this->addType('rtl', 'boolean');
75 82
    }
76
77
78 9
    public function setRead() {
79 9
        $this->markFieldUpdated('status');
80 9
        $this->status &= ~StatusFlag::UNREAD;
81 9
    }
82
83 18
    public function isRead() {
84 18
        return !(($this->status & StatusFlag::UNREAD) === StatusFlag::UNREAD);
85
    }
86
87 21
    public function setUnread() {
88 21
        $this->markFieldUpdated('status');
89 21
        $this->status |= StatusFlag::UNREAD;
90 21
    }
91
92 16
    public function isUnread() {
93 16
        return !$this->isRead();
94
    }
95
96 8
    public function setStarred() {
97 8
        $this->markFieldUpdated('status');
98 8
        $this->status |= StatusFlag::STARRED;
99 8
    }
100
101 18
    public function isStarred() {
102 18
        return ($this->status & StatusFlag::STARRED) === StatusFlag::STARRED;
103
    }
104
105 5
    public function setUnstarred() {
106 5
        $this->markFieldUpdated('status');
107 5
        $this->status &= ~StatusFlag::STARRED;
108 5
    }
109
110 2
    public function isUnstarred() {
111 2
        return !$this->isStarred();
112
    }
113
114
    /**
115
     * Turns entitie attributes into an array
116
     */
117 1
    public function jsonSerialize() {
118
        return [
119 1
            'id' => $this->getId(),
120 1
            'guid' => $this->getGuid(),
121 1
            'guidHash' => $this->getGuidHash(),
122 1
            'url' => $this->getUrl(),
123 1
            'title' => $this->getTitle(),
124 1
            'author' => $this->getAuthor(),
125 1
            'pubDate' => $this->getPubDate(),
126 1
            'body' => $this->getBody(),
127 1
            'enclosureMime' => $this->getEnclosureMime(),
128 1
            'enclosureLink' => $this->getEnclosureLink(),
129 1
            'feedId' => $this->getFeedId(),
130 1
            'unread' => $this->isUnread(),
131 1
            'starred' => $this->isStarred(),
132 1
            'lastModified' => $this->getLastModified(),
133 1
            'rtl' => $this->getRtl(),
134 1
            'intro' => $this->getIntro()
135 1
        ];
136
    }
137
138 7
    public function toAPI() {
139
        return [
140 7
            'id' => $this->getId(),
141 7
            'guid' => $this->getGuid(),
142 7
            'guidHash' => $this->getGuidHash(),
143 7
            'url' => $this->getUrl(),
144 7
            'title' => $this->getTitle(),
145 7
            'author' => $this->getAuthor(),
146 7
            'pubDate' => $this->getPubDate(),
147 7
            'body' => $this->getBody(),
148 7
            'enclosureMime' => $this->getEnclosureMime(),
149 7
            'enclosureLink' => $this->getEnclosureLink(),
150 7
            'feedId' => $this->getFeedId(),
151 7
            'unread' => $this->isUnread(),
152 7
            'starred' => $this->isStarred(),
153 7
            'lastModified' => $this->getLastModified(),
154 7
            'rtl' => $this->getRtl()
155 7
        ];
156
    }
157
158
159 4
    public function toExport($feeds) {
160
        return [
161 4
            'guid' => $this->getGuid(),
162 4
            'url' => $this->getUrl(),
163 4
            'title' => $this->getTitle(),
164 4
            'author' => $this->getAuthor(),
165 4
            'pubDate' => $this->getPubDate(),
166 4
            'body' => $this->getBody(),
167 4
            'enclosureMime' => $this->getEnclosureMime(),
168 4
            'enclosureLink' => $this->getEnclosureLink(),
169 4
            'unread' => $this->isUnread(),
170 4
            'starred' => $this->isStarred(),
171 4
            'feedLink' => $feeds['feed'. $this->getFeedId()]->getLink(),
172 4
            'rtl' => $this->getRtl()
173 4
        ];
174
    }
175
176
177 1
    public function getIntro() {
178 1
        return strip_tags($this->getBody());
179
    }
180
181 4
    public static function fromImport($import) {
182 4
        $item = new static();
183 4
        $item->setGuid($import['guid']);
184 4
        $item->setGuidHash($import['guid']);
185 4
        $item->setUrl($import['url']);
186 4
        $item->setTitle($import['title']);
187 4
        $item->setAuthor($import['author']);
188 4
        $item->setPubDate($import['pubDate']);
189 4
        $item->setBody($import['body']);
190 4
        $item->setEnclosureMime($import['enclosureMime']);
191 4
        $item->setEnclosureLink($import['enclosureLink']);
192 4
        $item->setRtl($import['rtl']);
193 4
        if($import['unread']) {
194 3
            $item->setUnread();
195 3
        } else {
196 1
            $item->setRead();
197
        }
198 4
        if($import['starred']) {
199 2
            $item->setStarred();
200 2
        } else {
201 2
            $item->setUnstarred();
202
        }
203
204 4
        return $item;
205
    }
206
207
208 22
    public function setAuthor($name) {
209 22
        parent::setAuthor(strip_tags($name));
210 22
    }
211
212
213 23
    public function setTitle($title) {
214 23
        parent::setTitle(strip_tags($title));
215 23
    }
216
217 13
    public function generateSearchIndex() {
218 13
        $this->setSearchIndex(
219 13
            strtolower(
220 13
                html_entity_decode(strip_tags($this->getBody())) .
221 13
                html_entity_decode($this->getAuthor()) .
222 13
                html_entity_decode($this->getTitle()) .
223 13
                $this->getUrl()
224 13
            )
225 13
        );
226 13
        $this->setFingerprint($this->computeFingerprint());
227 13
    }
228
229 13
    private function computeFingerprint() {
230 13
        return md5($this->getTitle() . $this->getUrl() . $this->getBody() .
231 13
                   $this->getEnclosureLink());
232
    }
233
234 24
    public function setUrl($url) {
235 24
        $url = trim($url);
236 24
        if(strpos($url, 'http') === 0 || strpos($url, 'magnet') === 0) {
237 17
            parent::setUrl($url);
238 17
        }
239 24
    }
240
241
242 26
    public function setBody($body) {
243
        // FIXME: this should not happen if the target="_blank" is already
244
        // on the link
245 26
        parent::setBody(str_replace(
246 26
            '<a', '<a target="_blank" rel="noreferrer"', $body
247 26
        ));
248 26
    }
249
250
}
251