Completed
Push — 16470-hide-recipients-on-bulk-... ( 9688d4 )
by Shawn
02:27
created

News   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 10.14 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 4
dl 7
loc 69
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A attachments() 0 4 1
A author() 0 4 1
A scopePublishedNews() 0 8 1
A setAttribute() 7 8 3
A getPublishDateAttribute() 0 4 1
A getExpirationDateAttribute() 0 4 1
A emailNews() 0 9 3

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 SET;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Facades\Mail;
8
use SET\Handlers\DateFormat;
9
use SET\Mail\SendNewsEmail;
10
11
class News extends Model
12
{
13
    use DateFormat;
14
15
    protected $table = 'news';
16
    public $timestamps = true;
17
18
    protected $fillable = ['title', 'description', 'publish_date', 'expire_date', 'author_id', 'send_email'];
19
20
    public function attachments()
21
    {
22
        return $this->morphMany('SET\Attachment', 'imageable');
23
    }
24
25
    public function author()
26
    {
27
        return $this->belongsTo('SET\User', 'author_id');
28
    }
29
30
    /**
31
     * @param $query
32
     */
33
    public function scopePublishedNews($query)
34
    {
35
        return $query->where('publish_date', '<=', Carbon::today())
36
                ->where(function ($q) {
37
                    $q->where('expire_date', '>=', Carbon::today())
38
                    ->orWhere('expire_date', null);
39
                });
40
    }
41
42
    /**
43
     * @param string $key
44
     * @param mixed  $value
45
     *
46
     * @return $this
47
     */
48 View Code Duplication
    public function setAttribute($key, $value)
49
    {
50
        if (is_scalar($value) && $value === '') {
51
            $value = null;
52
        }
53
54
        return parent::setAttribute($key, $value);
55
    }
56
57
    public function getPublishDateAttribute($value)
58
    {
59
        return $this->dateFormat($value);
60
    }
61
62
    public function getExpirationDateAttribute($value)
63
    {
64
        return $this->dateFormat($value);
65
    }
66
67
    /**
68
     * Email the news on the publish_date when a news article is created or updated.
69
     */
70
    public function emailNews()
71
    {
72
        $publishDate = Carbon::createFromFormat('Y-m-d', $this->publish_date);
73
74
        if ($this->send_email && $publishDate->eq(Carbon::now())) {
75
            $users = User::skipSystem()->active()->get();
76
            Mail::bcc($users)->send(new SendNewsEmail($this));
77
        }
78
    }
79
}
80