Completed
Push — master ( ac13d3...9f68b4 )
by
unknown
01:57
created

ForumPost::lastReply()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 11
nc 5
nop 0
1
<?php
2
3
namespace Taskforcedev\LaravelForum\Models;
4
5
use \Exception;
6
use Taskforcedev\LaravelForum\Helpers\UserHelper;
7
use \Validator;
8
9
/**
10
 * Class ForumPost
11
 *
12
 * @property integer $id
13
 * @property string  $title
14
 * @property string  $body
15
 * @property integer $forum_id
16
 * @property integer $author_id
17
 * @property integer $sticky
18
 * @property integer $locked
19
 *
20
 * @package Taskforcedev\LaravelForum\Models
21
 */
22
class ForumPost extends AbstractModel
23
{
24
    public $table = 'forum_posts';
25
26
    public $fillable = ['title', 'body', 'forum_id', 'author_id', 'sticky', 'locked'];
27
28
    /**
29
     * Eloquent Relation.
30
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
31
     */
32
    public function author()
33
    {
34
        $userHelper = new UserHelper();
35
        $model = $userHelper->getUserModel();
36
        return $this->belongsTo($model);
0 ignored issues
show
Bug introduced by
It seems like $model defined by $userHelper->getUserModel() on line 35 can also be of type boolean; however, Illuminate\Database\Eloquent\Model::belongsTo() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
37
    }
38
39
    /**
40
     * Eloquent Relation.
41
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
42
     */
43
    public function forum()
44
    {
45
        return $this->belongsTo('Taskforcedev\LaravelForum\Forum');
46
    }
47
48
    /**
49
     * Eloquent Relation.
50
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
51
     */
52
    public function replies()
53
    {
54
        $local = 'id';
55
        $foreign = 'post_id';
56
        return $this->hasMany('Taskforcedev\LaravelForum\ForumReply', $foreign, $local);
57
    }
58
59
    /**
60
     * Is model data valid.
61
     *
62
     * @param array|object $data The data to validate.
63
     *
64
     * @return boolean
65
     */
66
    public static function valid($data)
67
    {
68
        $rules = [
69
            'title' => 'required|min:3|max:100',
70
            'body' => 'required|min:5',
71
            'forum_id' => 'required|exists:forums,id',
72
            'author_id' => 'required|min:1|exists:users,id',
73
            'sticky' => 'min:1|max:1',
74
            'locked' => 'min:1|max:1',
75
        ];
76
        $validator = Validator::make($data, $rules);
77
78
        if ($validator->passes()) {
79
            return true;
80
        }
81
        return false;
82
    }
83
84
    /**
85
     * Gets the last reply for the current forum post.
86
     *
87
     * @return array
88
     */
89
    public function lastReply()
90
    {
91
        try {
92
            $replies = ForumReply::where('post_id', $this->id)->get();
93
94
            if ($replies->isEmpty()) {
95
                return null;
96
            }
97
98
            $reply = $replies->last();
99
100
            return [
101
                'author' => $reply->author->name,
102
                'date' => $reply->created_at
103
            ];
104
        } catch (Exception $e) {
105
            return null;
106
        }
107
    }
108
}
109