Completed
Branch master (8e0976)
by Adam
04:13
created

Log   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setTagsAttribute() 0 4 1
A getTagsAttribute() 0 4 1
A fillWithPost() 0 7 1
B isDirtyComparedToPrevious() 0 32 6
1
<?php
2
3
namespace Coyote\Post;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Coyote\Post;
7
8
/**
9
 * @property int $post_id
10
 * @property int $user_id
11
 * @property string $text
12
 * @property string $subject
13
 * @property array $tags
14
 * @property string $comment
15
 * @property string $ip
16
 * @property string $browser
17
 * @property string $host
18
 */
19
class Log extends Model
20
{
21
    /**
22
     * The attributes that are mass assignable.
23
     *
24
     * @var array
25
     */
26
    protected $fillable = ['post_id', 'user_id', 'text', 'subject', 'tags', 'comment', 'ip', 'browser', 'host'];
27
28
    /**
29
     * @var string
30
     */
31
    protected $dateFormat = 'Y-m-d H:i:se';
32
33
    /**
34
     * The database table used by the model.
35
     *
36
     * @var string
37
     */
38
    protected $table = 'post_log';
39
40
    /**
41
     * @var array
42
     */
43
    public $timestamps = false;
44
45
    protected $casts = ['tags' => 'json'];
46
47
    /**
48
     * @param $tags
49
     */
50
    public function setTagsAttribute($tags)
51
    {
52
        $this->attributes['tags'] = json_encode($tags);
53
    }
54
55
    /**
56
     * @param $value
57
     * @return mixed
58
     */
59
    public function getTagsAttribute($value)
60
    {
61
        return json_decode($value);
62
    }
63
64
    /**
65
     * @param Post $post
66
     * @return $this
67
     */
68
    public function fillWithPost(Post $post)
69
    {
70
        $this->fill($post->toArray());
71
        $this->post_id = $post->id;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Determine if post was somehow changed comparing to previous version
78
     *
79
     * @return bool
80
     */
81
    public function isDirtyComparedToPrevious()
82
    {
83
        if (empty($this->post_id)) {
84
            return true;
85
        }
86
87
        /** @var Log $previous */
88
        $previous = static::where('post_id', $this->post_id)->latest()->limit(1)->first();
89
        if (!$previous) {
90
            return true;
91
        }
92
93
        $diff = array_merge(
94
            array_diff($this->tags, (array) $previous->tags),
95
            array_diff((array) $previous->tags, $this->tags)
96
        );
97
98
        if (!empty($diff)) {
99
            return true;
100
        }
101
102
        $isDirty = false;
103
104
        foreach (['subject', 'text'] as $column) {
105
            if ($previous->{$column} !== $this->{$column}) {
106
                $isDirty = true;
107
                break;
108
            }
109
        }
110
111
        return $isDirty;
112
    }
113
}
114