Completed
Push — master ( bce3ff...b78e58 )
by Konstantinos
04:07
created

News::getCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file contains functionality relating to the news articles admins can post
4
 *
5
 * @package    BZiON\Models
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
/**
10
 * A news article
11
 * @package    BZiON\Models
12
 */
13
class News extends UrlModel implements NamedModel
14
{
15
    /**
16
     * The category of the article
17
     * @var int
18
     */
19
    protected $category;
20
21
    /**
22
     * The subject of the news article
23
     * @var string
24
     */
25
    protected $subject;
26
27
    /**
28
     * The content of the news article
29
     * @var string
30
     */
31
    protected $content;
32
33
    /**
34
     * The creation date of the news article
35
     * @var TimeDate
36
     */
37
    protected $created;
38
39
    /**
40
     * The date the news article was last updated
41
     * @var TimeDate
42
     */
43
    protected $updated;
44
45
    /**
46
     * The ID of the author of the news article
47
     * @var int
48
     */
49
    protected $author;
50
51
    /**
52
     * The ID of the last person to edit the news article
53
     * @var int
54
     */
55
    protected $editor;
56
57
    /**
58
     * The status of the news article
59
     * @var string
60
     */
61
    protected $status;
62
63
    /**
64
     * The name of the database table used for queries
65
     */
66
    const TABLE = "news";
67
68
    const CREATE_PERMISSION = Permission::CREATE_NEWS;
69
    const EDIT_PERMISSION = Permission::EDIT_NEWS;
70
    const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_NEWS;
71
    const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_NEWS;
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 2
    protected function assignResult($news)
77
    {
78 2
        $this->category = $news['category'];
79 2
        $this->subject = $news['subject'];
80 2
        $this->content = $news['content'];
81 2
        $this->created = TimeDate::fromMysql($news['created']);
82 2
        $this->updated = TimeDate::fromMysql($news['updated']);
83 2
        $this->author = $news['author'];
84 2
        $this->editor = $news['editor'];
85 2
        $this->status = $news['status'];
86 2
    }
87
88
    /**
89
     * Get the author of the news article
90
     * @return Player The author of the post
91
     */
92 1
    public function getAuthor()
93
    {
94 1
        return Player::get($this->author);
95
    }
96
97
    /**
98
     * Get the user ID of the author who wrote this article
99
     * @return int The author ID
100
     */
101
    public function getAuthorID()
102
    {
103
        return $this->author;
104
    }
105
106
    /**
107
     * Get the category of the news article
108
     * @return NewsCategory The category of the post
109
     */
110
    public function getCategory()
111
    {
112
        return NewsCategory::get($this->category);
113
    }
114
115
    /**
116
     * Get the database ID from the category the article belongs into
117
     * @return int The category ID
118
     */
119
    public function getCategoryID()
120
    {
121
        return $this->category;
122
    }
123
124
    /**
125
     * Get the content of the article
126
     * @return string The raw content of the article
127
     */
128
    public function getContent()
129
    {
130
        return $this->content;
131
    }
132
133
    /**
134
     * Get the time when the article was submitted
135
     *
136
     * @return TimeDate The article's creation time
137
     */
138
    public function getCreated()
139
    {
140
        return $this->created;
141
    }
142
143
    /**
144
     * Get the time when the article was last updated
145
     *
146
     * @return TimeDate The article's last update time
147
     */
148
    public function getLastEdit()
149
    {
150
        return $this->updated;
151
    }
152
153
    /**
154
     * Get the last editor of the post
155
     * @return Player A Player object of the last editor
156
     */
157
    public function getLastEditor()
158
    {
159
        return Player::get($this->editor);
160
    }
161
162
    /**
163
     * Get the ID of the person who last edited the article
164
     * @return int The ID of the last editor
165
     */
166
    public function getLastEditorID()
167
    {
168
        return $this->editor;
169
    }
170
171
    /**
172
     * Get the subject of the news article
173
     * @return string
174
     */
175 1
    public function getSubject()
176
    {
177 1
        return $this->subject;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 1
    public function getName()
184
    {
185 1
        return $this->getSubject();
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 1
    public static function getParamName()
192
    {
193 1
        return "article";
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 1
    public static function getRouteName($action = 'show')
200
    {
201 1
        return "news_$action";
202
    }
203
204
    /**
205
     * Update the content of a post
206
     *
207
     * @param string $content The new content of the post
208
     *
209
     * @return self
210
     */
211
    public function updateContent($content)
212
    {
213
        return $this->updateProperty($this->content, 'content', $content, 's');
214
    }
215
216
    /**
217
     * Update the last edit timestamp
218
     * @return self
219
     */
220
    public function updateEditTimestamp()
221
    {
222
        return $this->updateProperty($this->updated, 'updated', TimeDate::now(), 's');
223
    }
224
225
    /**
226
     * Update the editor of the post
227
     *
228
     * @param  int  $editorID The ID of the editor
229
     * @return self
230
     */
231
    public function updateLastEditor($editorID)
232
    {
233
        return $this->updateProperty($this->editor, 'editor', $editorID);
234
    }
235
236
    /**
237
     * Update the category of the post
238
     *
239
     * @param  int  $categoryID The ID of the category
240
     * @return self
241
     */
242
    public function updateCategory($categoryID)
243
    {
244
        return $this->updateProperty($this->category, 'category', $categoryID);
245
    }
246
247
    /**
248
     * Update the status of a post
249
     *
250
     * @param  string $status The new status of a post
251
     * @return self
252
     */
253
    public function updateStatus($status = 'published')
254
    {
255
        return $this->updateProperty($this->status, 'status', $status, 's');
256
    }
257
258
    /**
259
     * Update the subject of a post
260
     *
261
     * @param  string $subject The new subject of a post
262
     * @return self
263
     */
264
    public function updateSubject($subject)
265
    {
266
        return $this->updateProperty($this->subject, 'subject', $subject, 's');
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272 1
    public static function getActiveStatuses()
273
    {
274 1
        return array('published', 'revision');
275
    }
276
277
    /**
278
     * Add a new news article
279
     *
280
     * @param string $subject    The subject of the article
281
     * @param string $content    The content of the article
282
     * @param int    $authorID   The ID of the author
283
     * @param int    $categoryId The ID of the category this article will be published under
284
     * @param string $status     The status of the article: 'published', 'disabled', or 'deleted'
285
     *
286
     * @internal param int $categoryID The ID of the category
287
     * @return News An object representing the article that was just created or false if the article was not created
288
     */
289 2
    public static function addNews($subject, $content, $authorID, $categoryId = 1, $status = 'published')
290
    {
291 2
        return self::create(array(
292 2
            'category' => $categoryId,
293 2
            'subject'  => $subject,
294 2
            'content'  => $content,
295 2
            'author'   => $authorID,
296 2
            'editor'   => $authorID,
297 2
            'status'   => $status,
298 2
        ), 'issiis', array('created', 'updated'));
299
    }
300
301
    /**
302
     * Get all the news entries in the database that aren't disabled or deleted
303
     *
304
     * @param int  $start     The offset used when fetching matches, i.e. the starting point
305
     * @param int  $limit     The amount of matches to be retrieved
306
     * @param bool $getDrafts Whether or not to fetch drafts
307
     *
308
     * @return News[] An array of news objects
309
     */
310 1
    public static function getNews($start = 0, $limit = 5, $getDrafts = false)
311
    {
312 1
        $ignoredStatuses[] = "deleted";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ignoredStatuses was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ignoredStatuses = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
313
314 1
        if (!$getDrafts) {
315 1
            $ignoredStatuses[] = "draft";
316
        }
317
318 1
        return self::arrayIdToModel(
319 1
            self::fetchIdsFrom(
320 1
                "status", $ignoredStatuses, "s", true,
321 1
                "ORDER BY created DESC LIMIT $limit OFFSET $start"
322
            )
323
        );
324
    }
325
326
    /**
327
     * Get a query builder for news
328
     * @return QueryBuilder
329
     */
330 View Code Duplication
    public static function getQueryBuilder()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
331
    {
332
        return new QueryBuilder('News', array(
333
            'columns' => array(
334
                'subject'  => 'subject',
335
                'category' => 'category',
336
                'created'  => 'created',
337
                'status'   => 'status'
338
            ),
339
            'name' => 'subject'
340
        ));
341
    }
342
}
343