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

News::getPublishDateAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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