ContactTable::getContacts()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 32
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 28
c 1
b 0
f 0
nc 10
nop 0
dl 0
loc 32
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Adminetic\Contact\Http\Livewire\Admin\Contact;
4
5
use Carbon\Carbon;
6
use Livewire\Component;
0 ignored issues
show
Bug introduced by
The type Livewire\Component was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Livewire\WithPagination;
0 ignored issues
show
Bug introduced by
The type Livewire\WithPagination was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Support\Facades\Cache;
9
use Adminetic\Contact\Models\Admin\Group;
10
use Adminetic\Contact\Models\Admin\Contact;
11
12
class ContactTable extends Component
13
{
14
    use WithPagination;
15
16
    protected $paginationTheme = 'bootstrap';
17
18
    public $name;
19
    public $address = null;
20
    public $phone = null;
21
    public $email = null;
22
    public $gender = 1;
23
    public $active = 1;
24
    public $favorite = 0;
25
26
    public $search = '';
27
28
    public $filter;
29
30
    public $startDate;
31
32
    public $endDate;
33
34
    public $groups_id;
35
36
    public $group_filter_id = null;
37
38
    protected $listeners = ['date_range_filter' => 'dateRangeFilter', 'group_contacts' => 'groupContacts'];
39
40
    protected $rules = [
41
        'name' => 'required|max:80',
42
        'email' => 'required_if:phone,null|max:100',
43
        'phone' => 'required_if:email,null|numeric',
44
        'gender' => 'nullable|numeric',
45
        'address' => 'nullable|max:100',
46
        'favorite' => 'nullable|boolean',
47
        'active' => 'nullable|boolean'
48
    ];
49
50
    public function mount()
51
    {
52
        $this->filter = 1;
53
        /* Resets */
54
        $this->resetPage();
55
        $this->emit('initialize_contacts');
56
    }
57
58
    public function allContacts()
59
    {
60
        $this->filter = 1;
61
        /* Resets */
62
        $this->resetPage();
63
        $this->emit('initialize_contacts');
64
    }
65
66
    public function favoriteContacts()
67
    {
68
        $this->filter = 2;
69
        /* Resets */
70
        $this->resetPage();
71
        $this->emit('initialize_contacts');
72
    }
73
74
    public function nonFavoriteContacts()
75
    {
76
        $this->filter = 3;
77
        /* Resets */
78
        $this->resetPage();
79
        $this->emit('initialize_contacts');
80
    }
81
82
    public function activeContacts()
83
    {
84
        $this->filter = 4;
85
        /* Resets */
86
        $this->resetPage();
87
        $this->emit('initialize_contacts');
88
    }
89
90
    public function inActiveContacts()
91
    {
92
        $this->filter = 5;
93
        /* Resets */
94
        $this->resetPage();
95
        $this->emit('initialize_contacts');
96
    }
97
98
    public function dateRangeFilter($startDate, $endDate)
99
    {
100
        $this->filter = 6;
101
        $this->startDate = $startDate;
102
        $this->endDate = $endDate;
103
        /* Resets */
104
        $this->resetPage();
105
        $this->emit('initialize_contacts');
106
    }
107
108
    public function updatedSearch()
109
    {
110
        $this->filter = 7;
111
        /* Resets */
112
        $this->resetPage();
113
        $this->emit('initialize_contacts');
114
    }
115
116
    public function groupContacts($group_id)
117
    {
118
        $this->group_filter_id = $group_id;
119
        $this->filter = 8;
120
        /* Resets */
121
        $this->resetPage();
122
        $this->emit('initialize_contacts');
123
    }
124
125
    public function submit()
126
    {
127
        $contact = Contact::create($this->validate());
128
        if (isset($this->groups_id)) {
129
            $contact->groups()->attach($this->groups_id);
130
        }
131
        $this->emit('contact_created');
132
    }
133
134
    public function getContacts()
135
    {
136
        $filter = $this->filter;
137
        $contacts = Contact::latest()->paginate(10);
138
139
        if ($filter == 1) {
140
            return $contacts;
141
        } elseif ($filter == 2) {
142
            return Contact::favorite()->paginate(10);
143
        } elseif ($filter == 3) {
144
            return Contact::nonFavorite()->paginate(10);
145
        } elseif ($filter == 4) {
146
            return Contact::active()->paginate(10);
147
        } elseif ($filter == 5) {
148
            return Contact::inActive()->paginate(10);
149
        } elseif ($filter == 6) {
150
            $start = Carbon::create($this->startDate);
151
            $end = Carbon::create($this->endDate);
152
            return Contact::whereBetween('updated_at', [$start->toDateString(), $end->toDateString()])->paginate(10);
153
        } elseif ($filter == 7) {
154
            return Contact::where('name', 'LIKE', '%' . $this->search . '%')
155
                ->orWhere('address', 'LIKE', '%' . $this->search . '%')
156
                ->orWhere('phone', 'LIKE', '%' . $this->search . '%')
157
                ->orWhere('email', 'LIKE', '%' . $this->search . '%')
158
                ->paginate(10);
159
        } elseif ($filter == 8) {
160
            $group = Group::find($this->group_filter_id);
161
            if (isset($group)) {
162
                return $group->contacts()->paginate(10);
163
            }
164
        } else {
165
            return $contacts;
166
        }
167
    }
168
169
    public function render()
170
    {
171
        $contacts = $this->getContacts();
172
        $groups = Cache::get('groups', Group::latest()->get());
173
        return view('contact::livewire.admin.contact.contact-table', compact('contacts', 'groups'));
174
    }
175
}
176