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

Forum   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 23.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 14
loc 60
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A category() 0 4 1
A posts() 0 4 1
A getThreadCount() 0 9 2
A valid() 14 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Taskforcedev\LaravelForum\Models;
4
5
use \DB;
6
use \Validator;
7
8
/**
9
 * Class Forum
10
 *
11
 * @property integer $id          Forum ID.
12
 * @property string  $name        Forum Name.
13
 * @property string  $description Forum Description.
14
 * @property integer $category_id Category ID.
15
 * @property integer $public      Whether the forum is publicly viewable.
16
 *
17
 * @package Taskforcedev\LaravelForum\Models
18
 */
19
class Forum extends AbstractModel
20
{
21
    public $table = 'forums';
22
23
    public $fillable = ['name', 'description', 'category_id', 'public'];
24
25
    /**
26
     * Eloquent Relation.
27
     * @return mixed
28
     */
29
    public function category()
30
    {
31
        return $this->belongsTo('Taskforcedev\LaravelForum\ForumCategory');
32
    }
33
34
    /**
35
     * Eloquent Relation.
36
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
37
     */
38
    public function posts()
39
    {
40
        return $this->hasMany('Taskforcedev\LaravelForum\ForumPost');
41
    }
42
43
    /**
44
     * Get the number of threads inside the current forum.
45
     * @return mixed
46
     */
47
    public function getThreadCount()
48
    {
49
        try {
50
            $result = DB::select('SELECT COUNT(*) as count FROM `forum_posts` where forum_id = ?', [$this->id]);
51
            return $result[0]->count;
52
        } catch (\Exception $e) {
53
            return 0;
54
        }
55
    }
56
57
    /**
58
     * Is model data valid.
59
     *
60
     * @param array|object $data The data to validate.
61
     *
62
     * @return boolean
63
     */
64 View Code Duplication
    public static function valid($data)
65
    {
66
        $rules = [
67
            'name'        => 'required|min:3',
68
            'description' => 'min:3',
69
            'category_id' => 'required|min:0|integer|exists:forum_categories,id',
70
        ];
71
        $validator = Validator::make($data, $rules);
72
73
        if ($validator->passes()) {
74
            return true;
75
        }
76
        return false;
77
    }
78
}
79