Completed
Branch master (beb88f)
by Mohamed
05:24
created

CardView   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 2
cbo 4
dl 0
loc 72
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A card() 0 4 1
A setIpAttribute() 0 6 1
A getIpAttribute() 0 4 1
A getRules() 0 10 1
1
<?php
2
3
namespace Moo\FlashCard\Entity;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Validation\Rule;
7
use Moo\FlashCard\Traits\Validatable;
8
9
/**
10
 * CardView is the entity class that represents a record from the database.
11
 *
12
 * @author Mohamed Alsharaf <[email protected]>
13
 */
14
class CardView extends Model
15
{
16
    use Validatable;
17
18
    /**
19
     * Timestamp enabled.
20
     *
21
     * @var bool
22
     */
23
    public $timestamps = true;
24
25
    /**
26
     * Name of database table.
27
     *
28
     * @var string
29
     */
30
    protected $table = 'card_view';
31
32
    /**
33
     * List of allowed columns to be used in $this->fill().
34
     *
35
     * @var array
36
     */
37
    protected $fillable = ['card_id', 'ip'];
38
39
    /**
40
     * Get the card this view belong to
41
     */
42
    public function card()
43
    {
44
        return $this->belongsTo(Card::class);
45
    }
46
47
    /**
48
     * Set ip
49
     *
50
     * @param string $value
51
     * @return self
52
     */
53
    public function setIpAttribute($value)
54
    {
55
        $this->attributes['ip'] = ip2long(trim($value));
56
57
        return $this;
58
    }
59
60
    /**
61
     * Get ip
62
     *
63
     * @return string
64
     */
65
    public function getIpAttribute()
66
    {
67
        return long2ip($this->attributes['ip']);
68
    }
69
70
    /**
71
     * Get collection of validation rules for model attributes
72
     *
73
     * @return array
74
     */
75
    protected function getRules(): array
76
    {
77
        return [
78
            'ip' => [
79
                'required',
80
                // Ensure ip address unique per card
81
                Rule::unique($this->table, 'ip')->where('card_id', $this->card_id),
82
            ]
83
        ];
84
    }
85
}
86