Completed
Push — master ( 060b04...657701 )
by
unknown
04:35
created

Item   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 33.82%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 24
lcom 2
cbo 1
dl 0
loc 205
ccs 46
cts 136
cp 0.3382
rs 10
c 2
b 1
f 0

20 Methods

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