ContactService::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Faithgen\Miscellaneous\Services;
4
5
use Faithgen\Miscellaneous\Models\Contact;
6
use Illuminate\Database\Eloquent\Model as ParentModel;
7
use InnoFlash\LaraStart\Services\CRUDServices;
8
9
class ContactService extends CRUDServices
10
{
11
    private Contact $contact;
12
13
    public function __construct(Contact $contact)
14
    {
15
        if (request()->has('contact_id')) {
16
            $this->contact = Contact::findOrFail(request('contact_id'));
17
        } else {
18
            $this->contact = $contact;
19
        }
20
    }
21
22
    /**
23
     * Retrieves an instance of contact.
24
     */
25
    public function getContact(): Contact
26
    {
27
        return $this->contact;
28
    }
29
30
    /**
31
     * Makes a list of fields that you do not want to be sent
32
     * to the create or update methods.
33
     * Its mainly the fields that you do not have in the contacts table.
34
     */
35
    public function getUnsetFields()
36
    {
37
        return ['contact_id'];
38
    }
39
40
    /**
41
     * This returns the model found in the constructor
42
     * or an instance of the class if no contact is found.
43
     */
44
    public function getModel()
45
    {
46
        return $this->getContact();
47
    }
48
49
    /**
50
     * Attaches a parent to the current contact
51
     * You can delete this if you do not intent to create contacts from parent relationships.
52
     */
53
    public function getParentRelationship()
54
    {
55
        return [
56
            ParentModel::class,
57
            'relationshipName',
58
        ];
59
    }
60
}
61