Contact::scopeIsPublic()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Chuckcms\Contacts\Models;
4
5
use Chuckcms\Addresses\Traits\HasAddresses;
6
use Chuckcms\Contacts\Contracts\Contact as ContactContract;
7
use Chuckcms\Contacts\Exceptions\ContactDoesNotExist;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\SoftDeletes;
11
12
class Contact extends Model implements ContactContract
13
{
14
    use HasAddresses, SoftDeletes;
0 ignored issues
show
Bug introduced by
The trait Chuckcms\Addresses\Traits\HasAddresses requires the property $addresses which is not provided by Chuckcms\Contacts\Models\Contact.
Loading history...
15
16
    protected $guarded = ['id'];
17
18
    /**
19
     * The attributes that are fillable on this model.
20
     *
21
     * @var array
22
     */
23
    protected $fillable = [];
24
25
    /**
26
     * The default rules that the model will validate against.
27
     *
28
     * @var array
29
     */
30
    protected $rules = [];
31
32
    public function __construct(array $attributes = [])
33
    {
34
        $this->setTable(config('contacts.table_names.contacts'));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $this->setTable(/** @scrutinizer ignore-call */ config('contacts.table_names.contacts'));
Loading history...
35
        $this->mergeFillables();
36
37
        parent::__construct($attributes);
38
    }
39
40
    /**
41
     * Merge fillable fields.
42
     *
43
     * @return void.
0 ignored issues
show
Documentation Bug introduced by
The doc comment void. at position 0 could not be parsed: Unknown type name 'void.' at position 0 in void..
Loading history...
44
     */
45
    private function mergeFillables()
46
    {
47
        $fillable = $this->fillable;
48
        $columns = array_keys(config('contacts.fields.contacts'));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $columns = array_keys(/** @scrutinizer ignore-call */ config('contacts.fields.contacts'));
Loading history...
49
50
        $this->fillable(array_merge($fillable, $columns));
51
    }
52
53
    /**
54
     * Get the validation rules.
55
     *
56
     * @return array
57
     */
58
    public static function getValidationRules(): array
59
    {
60
        $rules = config('contacts.fields.contacts');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        $rules = /** @scrutinizer ignore-call */ config('contacts.fields.contacts');
Loading history...
61
62
        return $rules;
63
    }
64
65
    /**
66
     * Scope public contacts.
67
     *
68
     * @param \Illuminate\Database\Eloquent\Builder $builder
69
     *
70
     * @return \Illuminate\Database\Eloquent\Builder
71
     */
72
    public function scopeIsPublic(Builder $builder): Builder
73
    {
74
        return $builder->where('is_public', true);
75
    }
76
77
    /**
78
     * Scope primary contacts.
79
     *
80
     * @param \Illuminate\Database\Eloquent\Builder $builder
81
     *
82
     * @return \Illuminate\Database\Eloquent\Builder
83
     */
84
    public function scopeIsPrimary(Builder $builder): Builder
85
    {
86
        return $builder->where('is_primary', true);
87
    }
88
89
    public static function findById(int $id): ContactContract
90
    {
91
        $contact = static::where('id', $id)->first();
92
93
        if (!$contact) {
94
            throw ContactDoesNotExist::withId($id);
95
        }
96
97
        return $contact;
98
    }
99
}
100